reactjs - Redux 存储更新但没有重新渲染
问题描述
嗨,这里的第一篇文章,所以请和我一起轻松。我是 redux 的新手,想知道为什么我的商店成功更新后我没有重新渲染。
这是我的减速机
const initialState = {
pending: false,
loadPlanning: [],
error: null
}
export const loadPlanningReducer = (state = initialState, action) => {
if (action.type === INITIALISE_LOAD_PLANNING_PENDING) {
return {
...state,
pending: true
}
}
if (action.type === INITIALISE_LOAD_PLANNING_SUCCESS) {
console.log('updating state');
return Object.assign({}, state, {
...state,
pending: false,
loadPlanning: state.loadPlanning.concat(action.loadPlanning.items)
});
}
if (action.type === INITIALISE_LOAD_PLANNING_ERROR) {
return {
...state,
pending: false,
error: action.error
}
}
return state;
}
export default loadPlanningReducer;
export const getLoadPlanning = (state) => { console.log('reducer state',state); return state.loadPlanning };
export const getLoadPlanningPending = (state) => state.pending;
export const getLoadPlanningError = (state) => state.error;
视图看起来像
const mapStateToProps = (state, ownProps) => ({
error: getLoadPlanningError(state),
loadPlanning: getLoadPlanning(state),
pending: getLoadPlanningPending(state),
options: state.options.options,
option: state.options.currentOption,
oidc: state.oidc
})
const mapDispatchToProps = (dispatch) => {
return {
dispatch
};
}
const fetchLoadPlanning = async (props) => {
props.dispatch(initialiseLoadPlanning());
const httpOptions = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${xxxxxxxx}`
}
};
fetch('/Api/Planning/Get_Data?' + "database=xxxxxx", httpOptions)
.then(res => res.json())
.then(res => {
if (res.hasError) {
throw res.error;
}
props.dispatch(initialiseLoadPlanningSuccess(res.data));
return res.data;
})
.catch(error => {
props.dispatch(initialiseLoadPlanningError(error));
});
}
const LoadPlanningList = (props) => {
useEffect(() => {
fetchLoadPlanning(props);
}, [])
useEffect(() => {
console.log('props changed',props);
},[props])
}
道具更改的控制台日志发生在 props.pending 的更改上,而不是在 props.dispatch(initialiseLoadPlanningSuccess(res.data)) 的调度上;
您的帮助和智慧将是最有帮助的,在此先感谢
解决方案
推荐阅读
- java - 用Java解析JSON双括号
- python - 如何将 python 字符串数组转换为 POINTER(c_void_p)
- list - 列表视图中一次仅显示一项
- javascript - 在 iframe 中创建自定义元素
- javascript - 如何将两个json数组与object.assign结合起来
- python - 为什么pyqt5中的QPushButton很慢?
- bash - 如何在 tmux 中获取当前窗格下方的窗格索引?
- bash - 在 bash 中使用 While 循环读取一行时,它会将多个空间压缩为一个
- json - React - 将 JSON 呈现为 UI 对象并允许任何用户更改这些 UI 对象以更新 JSON
- git - Visual Studio Code 无法检测到已安装的 git 并且“启动 git 时出错:文件名或扩展名太长。”