【发布时间】:2021-10-14 03:49:34
【问题描述】:
我有问题。我想在加载 DOM 之前获取一些数据,但我已经遇到这个错误好几个小时了。我的查询有 200 个响应,但错误仍然存在。重新加载页面后显示正常。
// redux
const dispatch = useDispatch();
const customers = useSelector((state) => state.customerReducer);
useEffect( () => {
dispatch(findAllCustomers());
}, [])
{ !isEmpty(Object.values(customers)) && Object.values(customers)[0].map((customer, index) => ...
未捕获的类型错误:Object.values(...)[0].map 不是函数...
感谢您的帮助。
[Array(54)]
0: (54) [{…}, {…}, {…}, {…},
0: Array(54)
0: {id: 2,, …}
1: {id: 3, …}
2: {id: 4 , …}
//Actions.js
export const findAllCustomers = () => {
return (dispatch) => {
axios.get('/api/customers')
.then((response) => {
dispatch({
type: FIND_ALL_CUSTOMERS, payload:response.data
})
})
.catch((error) => console.log(error.response))
}
}
//CustomersReducer.js
const INITIAL_STATE = [];
function customerReducer(state = INITIAL_STATE, action)
{
switch (action.type){
case 'FIND_NB_CUSTOMERS' : {
return {
...state,
nbCustomers : action.payload
}
}
case 'FIND_ALL_CUSTOMERS' : {
return {
...state,
customers: action.payload
}
}
default:
return state
}
}
export default customerReducer;
//isEmpty()
export const isEmpty = (value) => {
console.log(value)
return (
value === undefined ||
value === null ||
(typeof value === "object" && Object.keys(value).length ===
0) ||
(typeof value === "string" && value.trim().length === 0)
);
}
【问题讨论】:
-
你确定你的意思是
Object.values(customers)[0]而不是Object.values(customers)没有[0]?这是一个数组还是数组? -
为什么要映射索引[0]?
-
向我们展示存储在
customers中的数据。没有它,所有答案都是推测性的猜测。 -
谢谢...我刚刚用收到的数据更新了我的帖子
-
@zero298 真正的问题是当我点击指向客户页面的链接时,数据来不及到达页面已经建立,当我刷新页面时显示数据正确 – Cedric Flamain 22 分钟前 删除
标签: reactjs dictionary redux react-router effect