Reactjs 如何在redux中存储同一查询的多个结果
我是一个新来的。。我正在尝试实现一些基本功能,查询数据库中的“系统类型”,并将结果存储在redux store中。。我当前实现的问题是,前一个查询的结果被当前查询的结果覆盖。。用户首先单击“系统类型x”并显示相应的配置文件信息,当用户单击“系统类型y”时,结果将覆盖redux store中的早期结果。。我不想为每个“系统类型”编写单独的方法或初始状态,因为新的“系统类型”可以在live中添加 是否有人能就如何实施该计划提供指导。。我已将我的关键文件复制到下面。。提前谢谢 RootReducerReactjs 如何在redux中存储同一查询的多个结果,reactjs,redux,react-redux,Reactjs,Redux,React Redux,我是一个新来的。。我正在尝试实现一些基本功能,查询数据库中的“系统类型”,并将结果存储在redux store中。。我当前实现的问题是,前一个查询的结果被当前查询的结果覆盖。。用户首先单击“系统类型x”并显示相应的配置文件信息,当用户单击“系统类型y”时,结果将覆盖redux store中的早期结果。。我不想为每个“系统类型”编写单独的方法或初始状态,因为新的“系统类型”可以在live中添加 是否有人能就如何实施该计划提供指导。。我已将我的关键文件复制到下面。。提前谢谢 RootReducer
const RootReducer = combineReducers({
getSystemProfile: GetSystemProfileReducer
})
GetSystemProfileReducer
const initialState = {
Error: null,
SystemProfile: []
}
const GetSystemProfileReducer = (state = initialState, action) => {
switch(action.type){
case 'GET_DATA':
const newstate = {...state, Error:'', SystemProfile: action.data};
return newstate
case 'GET_DATA_ERROR':
console.log("Fetch system data failed")
return {...state, Error: 'Fetch Failed'}
default:
return state
}
}
GetSystemProfileAction
export const getSystemProfile = (systemtype) => {
return (dispatch, getState) => {
fetch('https://xxx/profile/'+systemtype, {
method: 'get',
})
.then(response => response.json())
.then(data => {
dispatch({type : "GET_DATA", data})
})
.catch((err) => {
dispatch({type:'GET_DATA_ERROR', err})
});
}
}
SystemView.js
const mapStateToProps = state => {
return {
systemProfile: state.getSystemProfile.SystemProfile
}
}
const mapDispatchToProps = (dispatch) => {
return {
getSystemProfile: (systemtype) => dispatch(getSystemProfile(systemtype)),
}
}
不应忽略减速器上的systemtype, 我的建议是用map替换reducer上的数组,map的键是您的类型,值是列表
case 'GET_DATA':
const newstate = {...state, Error:'', state[action.payload.systemtype]: SystemProfile: action.payload.data};
return newstate
您的操作应该分派数据列表和系统类型
第二个选项是为列表中的每个项目添加systemtype,并使用新列表连接当前列表
SystemProfile: [...state.SystemProfile, action.data]
为什么不将新结果推到现有状态,比如
SystemProfile:[…state.SystemProfile,action.data]
,我希望你明白我的意思