我必须执行当过用户更新输入酒吧取回我的REST API的意图。我遇到的问题是,componentDidUpdate方法调用行动的创建者,其调度JSON的减速,又将更新状态,并再次调用componentDidUpdate。任何想法或最佳实践来结束无限循环?谢谢!阵营终极版无限循环
行动者:
export const fetchFoods = (dispatch, searchText) => {
return fetch(`/nutrition/${searchText}`)
.then(res => res.json())
.then(json => dispatch({type: 'RECEIVE_FOODS', json}))
}
减速机:
const foods = (state = [], action) => {
switch (action.type) {
case 'RECEIVE_FOODS':
return action.json
default:
return state
}
}
阵营集装箱:
const FoodList = React.createClass({
componentDidUpdate: function() {
fetchFoods(this.props.dispatch, this.props.searchText)
},
componentWillMount: function() {
fetchFoods(this.props.dispatch, this.props.searchText)
},
render: function() {
...
}
})
export default connect(
(state) => {
return {searchText: state.searchText, foods: state.foods}
}
)(FoodList)
你更新,这迫使更新后立即更新。 –
你可能想使用'mapDispatchToProps'与['connect'(https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options)来触发输入发生变化时的操作。还解释了[这里](http://redux.js.org/docs/basics/UsageWithReact.html)。 – robertklep