[英]react-redux with redux thunk with Router
I have this git repo i created https://github.com/markortiz905/emp-app我有这个 git 回购我创建了https://github.com/markortiz905/emp-app
Ive been practicing reactjs and wanted to learn about redux-thunk, at first kinda easy but I fall short on understanding how it works on routes as well.我一直在练习 reactjs 并想了解 redux-thunk,起初有点容易,但我也无法理解它在路由上的工作原理。
My investigation led me to think that data fetched from server is not triggering update component due to routing?我的调查使我认为从服务器获取的数据不会因为路由而触发更新组件?
If anyone have time to take a look on my repo its just few files and codes simple fetch empmloyee and display on view如果有人有时间查看我的存储库,它只有几个文件和代码,只需获取员工并显示在视图中
Heres my reducer.js snippet这是我的 reducer.js 片段
const initStates = {
employees: [],
loading: true
};
function rootReducer(state = initStates, action) {
console.log(state.employees);
if (action.type == UPDATE_EMPLOYEES) {
state.employees = action.payload;
} else if (action.type == LOADING) {
state.loading = action.payload;
}
//means something happen bad
return state;
}
I just found out whats wrong, it seems that I am doing it wrong from the very start in my reducer script This is wrong, I am updating employees from the const variable but const cant be updated right?我刚刚发现出了什么问题,似乎我从一开始就在我的减速器脚本中做错了这是错误的,我正在从 const 变量更新员工,但不能更新 const 对吗? once you've assigned a value to a variable using const, you can't reassign it to a new value.
一旦使用 const 为变量赋值,就不能将其重新赋值给新值。 source - https://tylermcginnis.com/var-let-const/
来源-https://tylermcginnis.com/var-let-const/
const initStates = {
employees: [],
loading: true
};
function rootReducer(state = initStates, action) {
console.log(state.employees);
if (action.type == UPDATE_EMPLOYEES) {
state.employees = action.payload;
} else if (action.type == LOADING) {
state.loading = action.payload;
}
//means something happen bad
return state;
}
I changed my reducer to return the new object instead.我更改了减速器以返回新的 object。
function rootReducer(state = initStates, action) {
switch (action.type) {
case UPDATE_EMPLOYEES_STARTED:
return {
...state,
loading: true,
employees: null,
};
case UPDATE_EMPLOYEES:
return {
...state,
loading: false,
error: null,
employees: action.payload,
};
case UPDATE_EMPLOYEES_ENDED:
return {
...state,
loading: false,
employees: [...state.employees],
};
default:
return state;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.