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?
Without adding a constructor you can stil use this. props in your components. It is not a must.
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 .
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.
No, you don't need to.
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.
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.
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';
you can access props without constructor in a class using "this", like this: class XXXXXX extends React.Component {
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
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>
)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With