사용자가 입력 막대를 업데이트 할 때 내 나머지 API로 가져 오기를 수행 할 의사가 있습니다. 내가 겪고있는 문제는 componentDidUpdate 메서드가 json을 감속기로 보내고 상태를 업데이트하고 componentDidUpdate를 다시 호출하는 작업 작성자를 호출한다는 것입니다. 끝없는주기를 끝내기위한 아이디어 또는 모범 사례? 감사!React Redux 무한 루프
액션 작성자 :
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)
업데이트 직후에 업데이트되므로 업데이트가 필요합니다. –
당신은 아마도''connect'' (https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options)와 함께'mapDispatchToProps'를 사용하여 트리거하려고합니다 입력이 변경되었을 때의 액션. 또한 [여기] (http://redux.js.org/docs/basics/UsageWithReact.html)에 설명되어 있습니다. – robertklep