Nisha Patel
Query: (React Getting error props.auth.data.map not a function after use modal to create a record. R…)
Problem
The parent component is a table that shows all the records I have for members in a table. I fetch that data from an API in Django. I have a modal that allows me to create a new member. I can create the new member but then it crashes and gives me an error that props.auth.data.map is not a function. If I reload the page then it will show me the new record in the table.
MemberList.js
Member.js
AddForm.js
store.js
auth.js (reducer)
auth.js (actions)
Payload from MEMBER_ADD_SUCCESS
Payload MEMBER_ADD_SUCCESS
Response
MEMBER_LIST_SUCCESS
Preview
Response
Solution
There is a mismatch of the response value between two of the API endpoints you are using. When you reload the page the GET request is made and the response value that is an array is used, but when adding a member, the POST response value is an object and replaces the array value that was in state.
The MEMBER_LIST_SUCCESS action is passed an array of objects:
While the MEMBER_ADD_SUCCESS action is passed an object:
MEMBER_ADD_SUCCESS appears to just be the member object that was added instead of the entire array of members with it included. Update the MEMBER_ADD_SUCCESS reducer case to append this value to the current state.auth.data array.
Example:
Suggested blogs:
>How to conditionally render different arrays in React?
>Fix react useState and array.map not work issue
>Making infinite scrolling using react-query
>How does React State with JSON arrays affect performance?
>setlist{nosep} not work in the acronym package
>Fix ScrollIntoView not working issue
>Testing react component with jest
>How you can Show or hide elements in React?
>Testing react components using Hooks and Mocks
>Tips for using React Redux Hooks to show or hide React components