【问题标题】:Stateful and Functional stateless components有状态和功能性无状态组件
【发布时间】:2018-09-29 10:48:47
【问题描述】:

如何将无状态功能组件转换为有状态组件,以便像无状态组件一样使用生命周期方法并将道具传递给它。

( export default JobCard = (props) => { 
   ............
 }
)

我需要将其转换为有状态组件,以便使用生命周期方法并将 props 传递给返回函数,就像这里传递 props 的方式一样。提前谢谢你。

【问题讨论】:

    标签: javascript reactjs react-native state stateless


    【解决方案1】:

    你可以这样做:

    export default class JobCard extends React.Component {
      render() {
    
        // here you can access props passing down from
        // parent components like: this.props
    
        return (
          <div>
            Hi, I'm a super smart component!
          </div>
        );
      }
    }
    

    【讨论】:

    • 谢谢。比如 let props = this.props ?
    • @PremKumar 请参阅 facebook.github.io/react-native/docs/props.html 了解更多关于 props 如何在 react-native 中工作的信息
    • 是的,你可以这样做。在无状态组件中,您可以访问 props.name 之类的道具,在有状态组件中,您可以像 this.props.name 一样访问
    • @konekoya 谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-11-18
    • 2017-05-02
    • 2019-03-24
    • 1970-01-01
    • 2016-05-30
    • 2016-04-17
    • 2021-12-15
    • 2016-06-08
    相关资源
    最近更新 更多