最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React - How to access props without using constructor - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Mar 3, 2019 at 7:24 Cœur 38.7k26 gold badges202 silver badges277 bronze badges asked Oct 19, 2017 at 18:03 SventiesSventies 2,7564 gold badges35 silver badges53 bronze badges 6
  • 2 Without adding a constructor you can stil use this.props in your components. It is not a must. – bennygenel Commented Oct 19, 2017 at 18:06
  • have you just tried to remove constructor? – Stanley Cheung Commented Oct 19, 2017 at 18:07
  • Wow. That works. But why, then, have I been doing super(props) for two years now... Is super(props) only required if you are creating a constructor for initialising state? – Sventies Commented Oct 19, 2017 at 18:07
  • Remove the constructor for now. You can later put that in when you start using state – Prakash Sharma Commented Oct 19, 2017 at 18:07
  • The constructor is called by React directly after the class is instantiated. It is only necessary if you wish to execute at that point in time, such as setting initial state. You need to call super(props) if you want to interact with the props 'in the constructor'. – bluesixty Commented Oct 19, 2017 at 18:14
 |  Show 1 more comment

2 Answers 2

Reset to default 16

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>
       )
   }
}
发布评论

评论列表(0)

  1. 暂无评论