Search code examples
reactjsreact-component

How to add props to a component instance in React.js?


I would like to know if it is posible to add a new prop to a component instance, the component instance look like this:

enter image description here

Maybe is a bad practice modifying a component instance directly, but I wonder if it is possible or not.


Solution

  • You can do this by using React.cloneElement:

    const changeProp = (component) => (
      {React.cloneElement(component, {
        someProp: { value }
      })}
    )

    Reference to react docs: https://reactjs.org/docs/react-api.html#cloneelement