user938363
user938363

Reputation: 10350

RN: constructor vs no constructor with props for component

In my React Native 0.59 App.js, 2 props are passed into each component:

const data = this.props.navigation.state.params.data;
const EventWithSelf = (props) => (<Event {...props} myself={data.myself} token={data.result} />)
const NeweventWithSelf = (props) => (<NewEvent {...props} myself={data.myself} token={data.result} />)

Since there is {...props} passed in for other props which may be needed for the component, do I have to initialize the component explicitly with constructor and run super(props) like below?

export default class Event extends React.Component {

    constructor(props) {
      super(props);
      this._isMounted = true;
      this.state = {
        activeEvents: [],
        user: this.props.myself,
        token: this.props.token,
      };

    };
     //more code

Or I can go without constructor like below as well:

export default class Event extends React.Component {

      state = {
        activeEvents: [],
        user: this.props.myself,
        token: this.props.token,
      };
      //more code.........  

Without explicit constructor, where is the better place to initialize this._isMounted = true?

Upvotes: 0

Views: 238

Answers (1)

mehulmpt
mehulmpt

Reputation: 16547

You don't need to have a constructor in place for this case. You can do something like this:

export default class Event extends React.Component {

      state = {
        activeEvents: [],
        user: this.props.myself,
        token: this.props.token,
      }
      _isMounted = false
}

Upvotes: 1

Related Questions