React - How to access details without using constuctor

Note. I am facing this specific issue using React Native, but I assume this applies to React in general.

I have a reaction component built using React.Component. I do not need to set the state, but I have the props. My suggested syntax was as follows:

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

I understand that I can use a function to create this component, for example:

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

But I prefer the first, because my component will grow, it may have state, etc., and I just want to keep all my components built in a similar way.

Now my linter is complaining about a useless constructor, but how else can I access the details by saving the class constructor instead of the function constructor?

+4
2

this.props , . , React .props .

(),

+8

"this", :

class XXXXXX extends React.Component {
   render() {
       return (
          <div>{this.props.value}</div>
       )
   }
}
0

Source: https://habr.com/ru/post/1687831/


All Articles