0

Im using React. and trying to assign an new key value. pair into the object. So first it goes through an if check and if it meets the requirement, I want to add the key value 'author'

if (field.component === 'Author') {
      this.props.writer.config.payload.name = 'Jefferson';
      console.log(this.props)
    }

There are some online articles that tell me to do this way and others that tell me to do Object.assign. Basically though, I just want to add the 'name':'Jefferson' into the object.

Kenny Quach
  • 225
  • 1
  • 5
  • 18

1 Answers1

0

See this answer https://stackoverflow.com/a/24943743/1964636

Mutating props in a child component is an anti pattern, You may want to pass a function as props that can lift state in the child component.

Once you modify the state of it's parent component (assuming the props are part of the parent component's state), it will rerender with updated props.

Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
Uzair Ashraf
  • 1,171
  • 8
  • 20