React: setting this.props in constructor ...? (do not try to mutate props)

I'm not trying to mutate the details. I just want to have access to this.props in the methods called in the constructor:

constructor(props){ super(props); this.props = props; // CAN I? There is no errors or warnings const filtered = this.filterValue(props.value); this.state = { value: filtered, } } filterValue(value){ // here I need this.props.* } 

I know that there are several ways to do this, for example. an additional argument is passed to filterValue, however I want it to be as simple as possible. I think the above code is pretty intuitive, the question is: can I do it like this? React will override this.props anyway.

+7
ecmascript-6 reactjs
source share
1 answer

You can access the details using this.props in the methods described in the React.Component documentation :

Instance Properties:

  • props

  • state

If you're interested, you can look at the source code of the ReactComponent constructor.

+5
source share

All Articles