简体   繁体   English

为什么我在 React 和 redux 上收到此错误“TypeError: state is not iterable”

[英]Why am i getting this error 'TypeError: state is not iterable' on react and redux

I'm trying since a few hours and still getting the same error "TypeError: state.codes is not iterable" when declaring the reducer几个小时后我一直在尝试,但在声明减速器时仍然遇到相同的错误“TypeError:state.codes is not iterable”

The idea behind my file is to pass a value (code) for ex.我的文件背后的想法是为 ex 传递一个值(代码)。 '12345' with dispatch into the reducer and be able to update an array _codeData every time when a new dispatch has taken place. '12345' 分配到 reducer 中,并且每次发生新的分配时都能够更新数组 _codeData。

anyone have any idea?有人知道吗?

thanks in advance:),提前致谢:),

File1.js文件1.js

function BarcodeScanner() {

  const initialState = {
    code: ''
  };

  const [state, dispatch ] = useReducer(reducer, initialState);

  function handleScan(code){

    dispatch({
      type: 'ADD_CODE',
      payload: {
        code,
      },
    });
  }

File2.js文件2.js

const initialState = {
    codes: []
};

export const reducer = (state, action) => {
    if (action.type === 'ADD_CODE') {
        return {
            ...state,
            codes: [...state.codes, action.payload.code],
        };
    }
    return state;
};

export default function ScannerResultTable() {
    
    const [state, dispatch] = useReducer(reducer, initialState);
    const [_codeData, setCodeData] = useState([]);

    useEffect(() => {
        setCodeData([...state.codes]);
    }, [state.codes]);

I guess you're trying to spread an undefined or null value.我猜您正在尝试传播未定义或 null 值。 To solve this issue you can use the Logical OR operator.要解决此问题,您可以使用逻辑或运算符。

PS: If the value was always null/undefined, ensure your reducer is working correctly first. PS:如果该值始终为 null/undefined,请首先确保您的 reducer 正常工作。

const initialState = {
    codes: []
};

export const reducer = (state, action) => {
    if (action.type === 'ADD_CODE') {
        return {
            ...state,
            codes: [(...state?.codes || []), action.payload.code],
        };
    }
    return state;
};

export default function ScannerResultTable() {
    
    const [state, dispatch] = useReducer(reducer, initialState);
    const [_codeData, setCodeData] = useState([]);

    useEffect(() => {
        setCodeData([(...state?.codes || [])]);
    }, [state.codes]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:状态在 react 和 redux 上不可迭代 - TypeError: state is not iterable on react and redux 为什么我没有从反应 redux state 获得更新的值 - Why I am not getting the updated value from react redux state 为什么会出现“ TypeError:无法读取未定义的属性” props”(反应路由器,React-Redux)? - Why am I getting 'TypeError: Cannot read property 'props' of undefined' (react-router, React-Redux)? React-Redux - 类型错误:state.menu 不可迭代 - React-Redux - TypeError: state.menu is not iterable 为什么我收到“TypeError: this.state.users.map is not a function”用于已部署的 React 应用程序生产版本 - Why am I getting a “TypeError: this.state.users.map is not a function” for deployed production version of React application 为什么会出现“未捕获的TypeError”错误? - Why am I getting 'Uncaught TypeError' error? 我在 redux 的调度之间收到 state 错误的突变 - I am getting mutation of state error between dispatches from redux 为什么我收到错误data.findIndex不是我的React with Redux项目中的函数? - Why am I getting the error data.findIndex is not a function in my React with Redux project? ReactJS & Redux - 类型错误:state.reduxCart 不可迭代 - ReactJS & Redux - TypeError: state.reduxCart is not iterable 为什么我在 redux-slice 中收到错误消息“Uncaught (in promise) TypeError: Cannot add property bookId, object is not extensible”? - Why I am getting an error as "Uncaught (in promise) TypeError: Cannot add property bookId, object is not extensible" into my redux-slice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM