2

What causes the ff issue ? Cannot assign to read only property '0' of object '[object Array]' ?

Any idea would be appreacited. Thanks.

#ts code snippet

 const [RegionalList, setRegionalList] = useState<IRegionalList[]>(RegionalListData);


 const setEmailValue = (event: any, regionalId: number, index: number) => {
    setRegionalList((prevState: IRegionalList[]) => {
      const newState = prevState.map((prop: IRegionalList) => {
        if (prop.id === regionalId) {
          prop.emails[index] = { emailAddress: event.target.value, id: null };
          return { ...prop };
        }
        return prop;
      });
      return newState;
    });
  }
7
  • prop.emails[index] = { .... } You are mutating the state here Commented Nov 10, 2022 at 13:25
  • if may I ask here about do avoid that ? and how do I fix t hat with my existing code above Commented Nov 10, 2022 at 13:28
  • yes Sir cause I need that muration to update the object Commented Nov 10, 2022 at 13:33
  • @adiga is right , and I think this will do what you want : return { ...prop,emails:[...prop.emails.filter( (_,i)=>i !== index ),{ emailAddress: event.target.value, id: null }] } Commented Nov 10, 2022 at 13:36
  • 1
    See stackoverflow.com/questions/64957735/… Commented Nov 10, 2022 at 14:00

1 Answer 1

1

here is the way that I suggest :

 const [RegionalList, setRegionalList] = useState<IRegionalList[]>(RegionalListData);


 const setEmailValue = (event: any, regionalId: number, index: number) => {
    setRegionalList((prevState: IRegionalList[]) => {
      const newState = prevState.map((prop: IRegionalList) => {
        if (prop.id === regionalId) {
         return { ...prop,emails:[...prop.emails.filter( (_,i)=>i !== index ),{ emailAddress: event.target.value, id: null }] } 
        }
        return prop;
      });
      return newState;
    });
  }

and if you care about order of your list you can do this :

    const setEmailValue = (event: any, regionalId: number, index: number) => {
    setRegionalList((prevState: IRegionalList[]) => {
      const newState = prevState.map((prop: IRegionalList) => {
        if (prop.id === regionalId) {
         let emails = prop.emails;
         emails[index] = { emailAddress: event.target.value, id: null };
          return { ...prop,emails }
        }
        return prop;
      });
      return newState;
    });
  }

please let me know if it fixed your problem

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Sir , just a question, what makes my existing code wrong ? and why is that it is not allowed to do that mutation ? Thanks.
Hi, I think this is because you cannot change value of state directly and prop.emails is value of state so you cannot use '=' to change its value what I did in second solution was just returning new value for emails and setRegionalList will set it per se if its not clear I can explain more

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.