Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I delete props in react

I just want to know is it possible to delete react component props safely from the component.

Is there any function like

this.destroyProps({someProp})

like image 242
Akhil P Avatar asked May 21 '16 01:05

Akhil P


People also ask

Why we should not change props in React?

The React philosophy is that props should be immutable and top-down. This means that a parent can send whatever prop values it likes to a child, but the child cannot modify its own props. What you do is react to the incoming props and then, if you want to, modify your child's state based on incoming props.

Can we change props in React?

A component cannot update its own props unless they are arrays or objects (having a component update its own props even if possible is an anti-pattern), but can update its state and the props of its children.

Are React props always objects?

React props can be accessed as an object or destructured Or they can be destructured, since props will always be an object, into separate variables. If you have a lot of props that you're passing down to your component, it may be best to include them on the entire props object and access them by saying props.


2 Answers

No, you can't. The props of a react component are immutable and are not supposed to be changed by the component.

If you need to work with the data locally, you could use the state of the component, or better create a local copy of the prop data.

like image 100
TimoStaudinger Avatar answered Oct 23 '22 03:10

TimoStaudinger


If you were passing the props onto a child component, you could create a new object, delete the properties from that and then pass to the child.

const childProps = { ...this.props };
delete childProps.someProp;
return (
  <ChildComponent {...childProps} />
)
like image 29
matharden Avatar answered Oct 23 '22 02:10

matharden