我試着給< ChipInput />組件從https://github.com/TeamWertarbyte/material-ui-chip-input我使用的材料界面反應組件和集成到目前爲止我有什麼是:反應,也能收到無法讀取的不確定上搜索輸入屬性「價值」
搜索輸入欄。當我鍵入藝術家時,它會返回結果。所以基本上是在工作。
現在,當我嘗試執行< ChipInput />,按照說明我沒有得到任何結果。 (注意IconButton和TextField在林被註釋掉的嘗試與ChipInput來代替它們)
因此,如果我輸入 「史密斯飛船」 我會得到:
FETCH_URL而不是https://itunes.apple.com/search?term=&limit=4
FETCH_URL https://itunes.apple.com/search?term=aerosmith&limit=4
所以,這就像沒有采取我的setState查詢的原因。我嘗試了componentWillReceiveProps,但它沒有幫助。有什麼建議麼 ?
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;
編輯:
順便說一句,如果我從< IconButton />(線110)取消註釋直到<的TextField />(線133)的端部確實準確我想要什麼,但沒有芯片(當然沒有ChipInput)
爲未來的讀者。正如我在我的答案(現在刪除)中寫道的,這個組件將一個字符串數組(不是事件)傳遞給'onChange'處理程序。但不知何故,這個事實並沒有與「這個搜索應用程序的整個想法」 –
,所以你說它不會採取一個事件。得到它了。我還能如何獲得我剛輸入的「術語」的價值,並從我的API中形成一個正確的URL? – brohymn