Skip to content
Advertisement

finalData is not a function

I’m trying to learn redux by creating multi step form (reactjs), everything is going fine, except when i click a button then all the data should go to one variable finalData, but its not going and i’m getting:

TypeError: finalData.map is not a function

When i check console.log(userData); i get all the data, but i want data to go to finalData, and changeFinalData is the one which updates it.

When i console log finalData i get: finalData => [...finalData, userData].

What am i doing wrong?

my code:

JavaScript

my appSlice.js:

JavaScript

DisplayData.js:

JavaScript

my App.js:

JavaScript

Advertisement

Answer

Issue

The issue here is that you are trying to implement your reducer logic in your UI code. You are storing a function in state, not actually updating state values.

JavaScript

This just stores the function (finalDataa) => [...finalDataa, userData] into state in the state.app.finalData state.

Solution

While you could just invoke the action payload function in your reducer and pass it the current finalData state

JavaScript

This is not how you’d want to maintain your state. You will instead want to pass in the action payload the data you want to update your state with.

JavaScript

And dispatch the action with the data payload.

JavaScript

Since you use similar logic in all your other reducer cases you will want to implement similar updates. This is so the reducer function maintains control over how state is updated.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement