有没有推荐的方法为组件提供私有属性/变量,更新Constructor中的值,并在整个组件中使用这些属性
示例:
export default class Workarea extends React.Component {
constructor(props) {
super(props);
MenuProps = this.props.navigation.getParam('MenuProps', {})
}
MenuProps= {};
getCurrentForm = () => {
if(MenuProps.Type == "Type1") {
this.RenderType1();
}
if(MenuProps.Type == "type2") {
this.RenderType2();
}
}
render() {
return (
<View>
{
this.getCurrentForm()
}
</View>
)
}
}
这是正确的基于类和属性的方法吗。或者我应该总是设置值和道具来说明请推荐。
我不熟悉ReactNative,但es6类不支持私有属性。不过,实现它们的方法是在类之外,但在同一个文件中声明变量,如下所示:
let _somePrivateProperty='hey';
export default class Workarea extends React.Component {
constructor(props) {
super(props);
MenuProps = this.props.navigation.getParam('MenuProps', {})
}
MenuProps= {};
getCurrentForm = () => {
if(MenuProps.Type == "Type1") {
this.RenderType1();
}
if(MenuProps.Type == "type2") {
this.RenderType2();
}
}
render() {
return (
<div>
<p>{_somePrivateProperty}</p>
<View>
{
this.getCurrentForm()
}
</View>
</div
)
}
}
希望这就是你的意思。