首页 > 解决方案 > 如何从 React 中的扩展类传递道具?

问题描述

最近我一直在一个项目中工作,我需要尽可能保持代码最干净。这就是为什么我在一个组件中工作的原因,该组件需要从另一个组件扩展一些特性,但我还需要传递一些道具来获得一些行为作为回报。但是有一个问题:我在构造函数中添加了一个道具,但是有一个函数没有接收/似乎没有那个道具:

class SampleClassOne extends PureComponent<Props>{
   Constructor(Props){
     console.log(props)    // In this console.log, the variable aditionalVar appears 
     super(props){
     {
   }
   functionOne(){
     const {
       varOne,
       varTwo,
       varThree,
       aditionalVar
     } = this.props
     console.log(this.props)  // In this console.log, the variable aditionalVar does not appear
     render(){
     ...
     }
   } 
}
class SampleClassTwo extends SampleClassOne {
   Constructor(Props){
     super({...props, aditionalVar: true})  // This is where I'm adding the new variable
     }
   }
}

我不知道我是否清楚我要问的问题......我如何确保我从其他扩展的组件添加的新变量可以被扩展其类的所有组件接收。

谢谢你的帮助

标签: javascriptreactjstypescript

解决方案


推荐阅读