每当我输入某些内容时,搜索栏就会更新状态,它会跳过第一个字母。例如,如果我写“ asdf”,则仅显示“ sdf”。
我在这段代码之前尝试过console.log
this.props.newQuery(this.state.newSearchQuery);
一切都很好。
请检查下面的App.js和SearchBar.js代码
谢谢
App.js
import React from 'react';
import SearchBar from './components/SearchBar';
class App extends React.Component {
constructor(){
super();
this.state = {
searchQuery: '',
fetchedData: []
};
}
newQuery(query){
this.setState({
searchQuery: query
});
}
onSearch(){
const userInput = this.state.searchQuery;
if(userInput !== '' && userInput !== ' '){
const API_KEY = `https://pokeapi.co/api/v2/pokemon/${userInput}`;
fetch(API_KEY, {
method: 'GET',
headers: {
Accept: 'application/json'
}
})
.then(result => result.json())
.then(data => this.setState({ fetchedData: data.results }));
console.log('res', this.state.fetchedData);
}
}
render(){
return(
<div className="App">
<h2>Search Pokemos by Types</h2>
<hr />
<SearchBar onSearch={this.onSearch.bind(this)} newQuery={this.newQuery.bind(this)} />
</div>
);
}
}
export default App;
SearchBar.js
import React from 'react';
class SearchBar extends React.Component {
constructor(props){
super(props);
this.state = {
newSearchQuery: '' //this blank value get executed first when i console.log
}
}
searchInput(event){
this.setState({
newSearchQuery: event.target.value
});
this.props.newQuery(this.state.newSearchQuery);
console.log(this.state.newSearchQuery); // if i log "asdf", state on top "newSearchQuery" skips the first letter, a and shows "sdf" only.
}
render(){
return(
<div className="input-group">
<input onChange={this.searchInput.bind(this)} className="form-control" placeholder="[eg. Ditto, Cheri, etc]" />
<button onClick={this.props.onSearch} className="btn btn-success">Search</button>
</div>
);
}
}
export default SearchBar;
答案 0 :(得分:0)
console.log
不会按预期方式记录日志,因为setState()
方法并不总是按其调用方式执行。根据{{3}}
状态更新可能是异步的:由于
this.props
和this.state
可能是异步更新的,因此您不应依赖它们的值来计算下一个状态。
因此,当您在console.log(this.state.newSearchQuery);
之后登录此setState
时,状态实际上并未更改,因此这就是为什么它记录意外的原因
答案 1 :(得分:0)
我对这两个组件之间的冗余状态有些困惑。我想我会做的(如果我没有使用mobx之类的东西)是将状态保留在父组件上,并将handleChange
和handleSearch
函数传递给<Search />
组件。看起来像...
我为您整理了一个代码框:https://codesandbox.io/s/n1ryz4rzwl
APP组件
import React, { Component } from 'react';
import SearchBar from './components/SearchBar'
class App extends Component {
constructor() {
super()
this.state = {
searchQuery: '',
fetchedData: []
}
}
handleChangeQuery = event => this.setState({searchQuery: event.target.value})
handleSearch = () => {
const {searchQuery} = this.state,
API_KEY = `https://pokeapi.co/api/v2/pokemon/${searchQuery}`;
fetch(API_KEY, {
method: 'GET',
headers: {
Accept: 'application/json'
}
})
.then(result => result.json())
.then(data => this.setState({ fetchedData: data.results }));
}
render() {
const {searchQuery} = this.state
return (
<div className="App">
<h2>Search Pokemos by Types</h2>
<hr />
<SearchBar
value={searchQuery}
handleChangeQuery={this.handleChangeQuery}
handleSearch={this.handleSearch}
/>
// Show fetchedData results here
</div>
)
}
}
export default App
SearchBar组件-这可以是无状态功能组件
import React from 'react'
const SearchBar = ({value, handleChangeQuery, handleSearch}) => {
return (
<div className="input-group">
<input
onChange={handleChangeQuery}
value={value}
className="form-control"
placeholder="[eg. Ditto, Cheri, etc]"
/>
<button onClick={handleSearch} className="btn btn-success">Search</button>
</div>
)
}
export default SearchBar
其他注释已说明了奇怪的丢失字符背后的原因-因为this.setState()
可能是异步的。但是,this.setState()
确实具有回调函数,如果您要进行测试,可以使用该函数来确认更改。看起来像这样:
this.setState({key: value}, () => {
// State has been set
console.log(this.state.key)
})