Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - How to access props without using constructor

Note: I encounter this specific problem using React Native, but I guess this goes for React in general as well.

I have a react component built using React.Component. I don't need to set state, but I do have props. My proposed syntax was as follows:

class Header extends Component {
  constructor(props) {
    super(props);
  }
  render() {
    return <div>{this.props.title}</div>;
  }
}

I understand I can use a function to construct this component, like this:

const Header = (props) => {
  return <div>{props.title}</div>;
}

But I prefer the former, because my component will grow, might have state etc, and I just want to keep all my components built in a similar fashion.

Now, my linter complains about having a useless constructor, but how else do I access the props while keeping a class constructor instead of a function constructor?

like image 946
Sventies Avatar asked Oct 19 '17 18:10

Sventies


People also ask

Can I use props without constructor?

Without adding a constructor you can stil use this. props in your components. It is not a must.

How do I access React props?

React props can be accessed as an object or destructured 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. propName .

Is it necessary to use constructor in React?

If you don't initialize state and you don't bind methods, you don't need to implement a constructor for your React component. The constructor for a React component is called before it is mounted. When implementing the constructor for a React.Component subclass, you should call super(props) before any other statement.

Is constructor mandatory in class component in React?

No, you don't need to.

Why is the constructor of a React component called props?

class Main extends React.Component { constructor (props) { //etc, etc... } // etc, etc... } Can anyone tell me why? All the attributes of a react component in JSX are called “props”. So when you have a component like: It will receive an object as the props parameter, with bar and xyzzy keys. In other words, it’s automatically passed for you.

How to access the state and props in react?

The state and props in React are always in an object format. This means that the value could be accessed from the state and props via key-value pair. To access the normal state object, you can use the key name from the object. The state object will look as shown below.

How do I set state in react without constructor?

React State without a Constructor In React, state is used in a React class component. There you can set initial state in the constructor of the class, but also access and update it with this.state and this.setState, because you have access to the class instance by using the this object. import React, { Component } from 'react';

How to access props without constructor in a class?

you can access props without constructor in a class using "this", like this: class XXXXXX extends React.Component {


2 Answers

If you want to use this.props in the constructor, you need to pass props to super. Otherwise, it doesn't matter because React sets .props on the instance from the outside immediately after calling the constructor.

So just simply remove constructor() if useless

like image 81
Stanley Cheung Avatar answered Nov 09 '22 02:11

Stanley Cheung


you can access props without constructor in a class using "this", like this:

class XXXXXX extends React.Component {
   render() {
       return (
          <div>{this.props.value}</div>
       )
   }
}
like image 44
Adolfo Onrubia Avatar answered Nov 09 '22 00:11

Adolfo Onrubia