Home > front end >  TypeError: Object is not iterable - when using useSelector in Redux
TypeError: Object is not iterable - when using useSelector in Redux

Time:09-22

I am trying to use my isFetching and error in my component. However, I am getting the error in the title when trying to select the user from the state using useSelector. Here's my code:

  const [username, setUsername] = useState("");
  const [password, setPassword] = useState("");
  const dispatch = useDispatch();
  const [isFetching, error] = useSelector((state)=>state.user)
  

  const handleClick = (e) => {
    e.preventDefault();
    login(dispatch, { username, password });
  };

And the error message:

TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator))
Login
C:/Users/xxx/Shop/E-commerce/src/pages/Login.jsx:74
  71 |  const [username, setUsername] = useState("");
  72 |  const [password, setPassword] = useState("");
  73 |  const dispatch = useDispatch();
> 74 |  const [isFetching, error] = useSelector((state)=>state.user)
     | ^  75 |  
  76 | 
  77 |  const handleClick = (e) => {

CodePudding user response:

Hey I looked at your question and you just need to do a simple change in your code just like the error says object is not iterable we have to use '{}' in this line below!

In your code change this:

From :
  const [isFetching, error] = useSelector((state)=>state.user)

To :

  const {isFetching, error} = useSelector((state)=>state.user)

You need to work with object of array.

  • Related