0

The code below is what I'm trying to do update main_id and sub_ids in the state.

I got stucked from here...

const [state, setState] = useState({
    ids: [
      {
        main_id: null,
        sub_ids: []
      }
    ]
  });

//this is what I've tried..
const handleState = index => (v) => {
  setState({...setState, ids: setState.ids.map((x,i)=>(
      i === index ? {x.main_id: v.id, sub_ids: []})

    ))})
}

I'm using this function on same component which means it adds in specific index of array with different object.

<componentOne onChange ={handleState(k)}/>
<componentTwo onChange={handlestate(k)} />

The state that I desired to get after this,


state ={
ids:[
      {
     main_id: v.id,
     sub_ids:[]
      },
      {
     main_id: v.id,
     sub_ids:[]
      }
   ]
}

1 Answer 1

3

Looks like you are trying to spread in your state updater function (setState) versus your current state object (state).

Shallow copy existing state, then also shallow copy the element when the index matches. You should also use a functional state update.

const handleState = index => v => {
  setState(state => ({
    ...state, // <-- copy previous state
    ids: state.ids.map((x, i) =>
      i === index
        ? {
            ...x, // <-- on match, copy element, then update properties
            main_id: v.id,
            sub_ids: [],
          }
        : x, // <-- non-match, just pass element through
    ),
  }));
};

It may be a little clearer to simplify your state though since it appears you overwrite the entire element object.

const [ids, setIds] = useState([
  {
    main_id: null,
    sub_ids: []
  }
]);

const handleState = index => v => {
  setIds(ids => ids.map((x, i) =>
      i === index
        ? {
            main_id: v.id,
            sub_ids: [],
          }
        : x,
    ),
  }));
};
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you, It works perfectly. But can I ask one more question. So I'm using this function for other same component. What should I do if I want to add same structure of 'ids' on ext index of it? I edited desired result
@HK Sorry, I don't quite follow the question. Are you asking how to append new "empty" entries into your state data?
I want to use this function on same component. components are currently generated by map function with index
@HK Ah, I see. Do both components render the same data in the same way? i.e. is the kindex k going to be the same for both? Or is all the same component, just different instances of it from the map function? It may be easier to just update your question to include a snippet of how you are mapping to components and attaching the handleState callback.
k is incremented by +1. And Yes, same component and similar data but different instances of it from the map function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.