reactjs 代码axios中的问题在哪里,获取“response.data”?

amrnrhlw  于 2023-06-05  发布在  React
关注(0)|答案(1)|浏览(474)

设置数据:

export const setIngredients = (ingredients) => {return { type: actionTypes.SET_INGREDIENTS,ingredients : ingredients}}

获取数据:

export const initIngredients = () => {return dispatch => {axios.get('....').then(response => {dispatch(setIngredients(response.data));}).catch( error => {dispatch(fetchIngredientsFailed())})} }

错误:

Objects are not valid as a React child (found: object with keys {objectId, name, createdAt, updatedAt, value}). If you meant to render a collection of children, use an array instead.
return dispatch => { axios.get('......').then(response => {dispatch(setIngredients(response.data));{dispatch(fetchIngredientsFailed())}

6qqygrtg

6qqygrtg1#

这似乎不是你的API获取Axios代码的问题,而是渲染逻辑的问题。
在JSX代码中的某个地方,您直接引用了ingredients的对象/数组,这就是它给出此错误的原因。因为react不支持对象或数组作为子组件。
您也可以参考此问题,Objects are not valid as a React child. If you meant to render a collection of children, use an array instead

相关问题