我并不想改变 Prop 。 我只想在构造函数中调用的方法中访问 this.props:
constructor(props){
super(props);
this.props = props; // CAN I? There is no errors or warnings
const filtered = this.filterValue(props.value);
this.state = {
value: filtered,
}
}
filterValue(value){
// here I need this.props.*
}
我知道有几种方法可以做到这一点,例如传递给 filterValue 的附加参数,但我希望它尽可能简单。我认为上面的代码是 很直观,问题是:我可以这样做吗?无论如何,React 都会覆盖 this.props。
最佳答案
您可以在方法中使用 this.props
访问 Prop ,如 the React.Component
documentation 中所述。 :
Instance Properties:
props
state
如果你好奇的话,可以看一下ReactComponent
constructor的源代码.
关于reactjs - React 构造函数中的 : Setting this. 属性...? (不试图改变 Prop ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38567086/