我是 redux 世界的新手,我正在尝试制作报纸应用程序。我目前正在研究用户可以搜索特定报纸标题的搜索功能。问题是当我第一次输入例如'a'时,状态是''。当我输入更多例如'b'时,状态显示该术语是'a',而它应该是'ab'。我正在使用 redux chrome 工具来检查这个全局状态。
我的 Actioncreator 非常简单(只返回传递给它的术语):
export function search(term) {
return{
type:SEARCH,
term
}
}
这是减速器:
const SearchReducer = (state = '', action) => {
switch (action.type) {
case SEARCH:
return action.term;
default:
return state;
}
}
这是根减速器:
/*Application state*/
const rootReducer = combineReducers({
weather: WeatherReducer,
filters: FilterReducer,
articles:ArticleReducer,
search: SearchReducer // this should be 'ab', but is 'a'
});
这是非常简单的设置。这是我与 redux 通信的方式。
我有我的 material-ui 文本字段(我也尝试过香草输入字段)
<TextField style={style.searchWidget.search}
floatingLabelText="Search"
value={this.state.term}
onChange={this._onSearchChange}
/>
当用户输入内容时,_onSearchChange 函数被触发。
_onSearchChange(event) {
this.setState({term: event.target.value});
this.props.search(this.state.term);
}
这将为此搜索组件设置当前状态。然后它将调度将更新全局状态的搜索操作。但不能正常工作。全局状态总是落后 1 步。
任何的想法?
编辑:看起来它不是 redux 而是react。组件状态不会立即更新。正确的术语被传递给 actionreducer,所以这不是 redux 的错。我尝试在设置状态后打印出 this.state.term。看起来状态没有更新。