[英]React, Keep getting Cannot read property 'value' of undefined on Search Input
Im trying to integrate the < ChipInput /> component from https://github.com/TeamWertarbyte/material-ui-chip-input I'm using material-UI react component and so far what I have is: 我正在尝试从https://github.com/TeamWertarbyte/material-ui-chip-input集成<ChipInput />组件,我正在使用material-UI react组件,到目前为止,我所拥有的是:
A search input bar. 搜索输入栏。 When I type an artist it returns a result.
当我键入一个艺术家时,它将返回结果。 so basically is working.
所以基本上是可行的。
Now, when I try to implement < ChipInput /> , following the instructions I get no results. 现在,当我尝试实现<ChipInput />时 ,按照说明进行操作没有任何结果。 (note IconButton and TextField are commented out as in Im trying to replace them with ChipInput)
(注意,ImButton和TextField被注释掉,因为我试图用ChipInput替换它们)
Therefore, if I type "aerosmith" I'll get: 因此,如果键入“ aerosmith”,我将得到:
FETCH_URL https://itunes.apple.com/search?term=&limit=4 instead of FETCH_URL https://itunes.apple.com/search?term=&limit=4而不是
FETCH_URL https://itunes.apple.com/search?term=aerosmith&limit=4 FETCH_URL https://itunes.apple.com/search?term=aerosmith&limit=4
so , it's like is not taking my setState query for a reason. 所以,这就像是由于某种原因而不接受我的setState查询。 I tried componentWillReceiveProps but it didnt help.
我尝试了componentWillReceiveProps,但没有帮助。 Any suggestions ?
有什么建议么 ?
class Searcher extends React.Component {
constructor(props){
super(props);
this.state = {
query: [],
application: null,
}
}
componentDidMount () {
this.handleSearchRequest();
}
handleSearchRequest() {
console.log('this.state', this.state);
// we will replace BASE_URL with Anu's search api
const BASE_URL = 'https://itunes.apple.com/search?';
const FETCH_URL = BASE_URL + 'term=' + this.state.query + '&limit=4';
console.log('FETCH_URL', FETCH_URL);
fetch(FETCH_URL, {
method: 'GET'
})
// Initial test to see what the console response is after hiting the API
// .then(response => console.log('response', response));
.then(response => response.json())
// const applications = response.data
// this.setState({applications})
//testing the json results
.then(json => {
// console.log('json', json)
const application = json.results[0];
this.setState({application})
console.log({application})
});
}
handleChange(event) {
this.setState({ query: event.target.value})
}
render () {
return (
<div style={{position: 'relative'}}>
{/* <IconButton
iconStyle={styles.smallIcon}
style={styles.iconButton}
onClick={() => this.handleSearchRequest()}
>
<Search color={black} />
</IconButton>
<TextField
underlineShow={false}
id="searchId"
value={this.state.query}
fullWidth={true}
style={styles.textField}
inputStyle={styles.inputStyle}
hintStyle={styles.hintStyle}
onChange={event => {this.setState({ query: event.target.value}) }}
onKeyPress={event => {
if (event.key === 'Enter') {
this.handleSearchRequest()
}
}}
/> */}
<br/>
<br/>
<ChipInput
fullWidth={true}
defaultValue={this.state.query}
onChange={(event) => this.handleChange(event)}
/>
{
this.state.application != null
?
<ResultItem
{...this.props} {...this.state}
application={this.state.application}/>
: <div></div>
}
</div>
);
}
}
export default Searcher;
EDIT: 编辑:
By the way, if I uncomment from < IconButton /> (line 110) til the end of < TextField /> (line 133) it does exactly what I want , but no chips ( with no ChipInput of course) 顺便说一句,如果我从<IconButton /> (第110行)取消注释,直到<TextField /> (第133行)的末尾,它完全可以实现我想要的功能,但是却没有芯片(当然也没有ChipInput)
You dont need a defaultValue all you need is 4 things (if you want autocomplete) searchText, dataSource, onUpdateInput and Onchange. 您不需要defaultValue,您只需要4件事(如果您需要自动完成)searchText,dataSource,onUpdateInput和Onchange。
Material UI autocomplete and chips properties are similar so apply them to ChipInput, they are almost the same. 材质UI自动完成和芯片属性相似,因此将它们应用于ChipInput,它们几乎相同。
Bottom line is you have to write a function for every property you use within ChipInput expect for searchText, which can actually be a string. 最重要的是,您必须为在ChipInput中期望使用searchText的每个属性编写一个函数,该属性实际上可以是一个字符串。 As you can see, ChipInput might be easy with hardcoded values, but when you start hitting APIS it is not so easy anymore.
如您所见,ChipInput使用硬编码的值可能会很容易,但是当您开始使用APIS时,它就不再那么容易了。 it is important to realize what onUpdateInput does
重要的是要意识到onUpdateInput的作用
Also, you are supposed to bind every function you write within the constructor this is a react pattern that ensures performance, found it on a book. 同样,您应该将在构造函数中编写的每个函数绑定在一起,这是一种反应模式,可以确保性能,可以在书中找到它。
constructor(props) {
super (props)
this.onUpdateInput = this.onUpdateInput.bind(this);
this.onNewRequest = this.onNewRequest.bind(this);
}
Then on the render method 然后在render方法上
<ChipInput
searchText={this.state.query}
fullWidth={true}
dataSource={this.state.dataSource}
onUpdateInput={this.onUpdateInput}
onChange={this.onNewRequest}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.