我是REDX世界的新手,我試圖製作報紙應用程序。我目前正在研究用戶可以搜索特定報紙標題的搜索功能。問題是,當我第一次輸入'a'時,狀態是''。而當我輸入更多,例如'b'時,狀態表明當它應該是'ab'時,該術語是'a'。我正在使用redux chrome工具來檢查這個全局狀態。Redux/React - 搜索字段沒有立即更新REDX狀態
我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溝通。
我有我的材料UI文本框(我試過香草輸入字段太)
<TextField style={style.searchWidget.search}
floatingLabelText="Search"
value={this.state.term}
onChange={this._onSearchChange}
/>
當用戶鍵入的東西的_onSearchChange func被解僱。
_onSearchChange(event) {
this.setState({term: event.target.value});
this.props.search(this.state.term);
}
這將設置此搜索組件的當前狀態。然後它將調度將更新全局狀態的搜索操作。但不能正常工作。全球國家總是落後一步。
有什麼想法?
編輯: 它看起來不是重複但反應。組件狀態不會立即更新。正確的術語被傳遞給actionreducer,所以它不是redux的錯。設置狀態後,我試圖打印出this.state.term。而且看起來狀態並未更新。
感謝您的提示。它效果不錯:) – onurhb