gpt4 book ai didi

javascript - 无法添加属性,在 React 组件中绑定(bind) Prop 时对象不可扩展

转载 作者:行者123 更新时间:2023-11-29 21:05:23 24 4
gpt4 key购买 nike

出于某种原因,当我尝试将一个函数绑定(bind)到我的 React 组件中的一个 prop 时,它出现了

TypeError: Can't add property onSearch, object is not extensible.

我不太了解这意味着什么或它出现的原因,我认为这可能与我仍在寻找解决方法的 es6 绑定(bind)有关。

这是两个最相关的组件。

搜索框

import React from 'react';

import SearchForm from 'SearchForm';
import searchDisplay from 'searchDisplay';
import googleRequests from 'googleRequests';

class SearchBox extends React.Component {
constructor() {
super();
this.state = {
searchResults: []
}
this.handleSearch = this.handleSearch.bind(this);


}
handleSearch(searchTerm) {
googleRequests.search(searchTerm).then((response) => {
console.log(response.items);
this.extrapolateResults(response.items);
}), ((error) =>{
console.log(error)
});
}
//pull relevant data out of api call
extrapolateResults(arr) {
function Book(objInArr) {
this.link = objInArr.selfLink;
this.bookTitle = objInArr.volumeInfo.title;
this.author = objInArr.volumeInfo.authors;
this.bookDescription = objInArr.volumeInfo.description;
this.thumbnail = function() {
if (objInArr.volumeInfo.hasOwnProperty('imageLinks')){
return objInArr.volumeInfo.imageLinks.smallThumbnail
}
else {
return "No Thumbnail Available";
}
};
this.thumbnailPic = this.thumbnail();
}
//push extrapolated data into array
var finalRes = [];
var initRes = arr;
initRes.forEach(function (objInArr) {
var obj = new Book(objInArr);
finalRes.push(obj);
})
this.setState({
searchResults: finalRes
})
console.log(finalRes, this.state.searchResults)

}


render() {
var res = this.state.searchResults;

function renderResults() {
if (res.length !== 0) {
return (<SearchDisplay content={res} />)
}
else {
return;
}
}

var style = {
border: '1px solid black',
height: '80%',
width: '83%'
}
return (
<div style={style}>
<SearchForm onSearch={this.handleSearch}> </SearchForm>
</div>)
}
};

export default SearchBox;

搜索表单

import React from 'react';

class SearchForm extends React.Component {
constructor(props) {
super(props);
this.onFormSubmit = this.onFormSubmit.bind(this);
this.props.onSearch = props;
}
onFormSubmit(e) {
e.preventDefault();
var searchTerm = this.refs.searchTerm.value;
if (searchTerm.length > 0) {
this.refs.searchTerm.value = '';
this.props.onSearch(searchTerm);
}
}

render() {
var style = {
border: '1px solid black',
float: 'left',
height: '100%',
width: '30%'
}

return(
<div style={style} className="container">
<form onSubmit={this.onFormSubmit}>
<input type="text" placeholder="search name here" ref="searchTerm"/>
<input type="submit" className="button" value="Get Book"/>
</form>
</div>
);
}
};

export default SearchForm;

我觉得我遗漏了一些非常简单的东西,但是在谷歌搜索这个问题一段时间后,我仍然无法弄清楚它是什么......

最佳答案

删除 this.props.onSearch = props;...不确定你想在那行做什么

关于javascript - 无法添加属性,在 React 组件中绑定(bind) Prop 时对象不可扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44364907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com