Search code examples
reactjscomponentsstatereact-props

How to change the prop content in a component on button click?


I click the button and the function call should change the props inside the component. Can this be done and how?


Solution

  • we cannot change the props means we can assign them to another value but the component won't be updated.

    Also, props should not be changed, they are read-only

    If you want to change then do following

    • if props is a state in the parent then pass its setter in the child
    • else store the prop as a state in a child component

    video take reference from this video