関数をactions.jsファイルにエクスポートしたいだけですが、動作できません。 これは、作業台です:対応するネイティブコンポーネントから関数をエクスポートする
export default class HomeScreen extends Component {
constructor(props) {
super(props);
this.state = {
visible: false
}
}
onOpen =() => {
this.setState({visible:true});
console.log(this.state.visible);
}
render() {
return (
<View style={styles.container}>
<Button onPress={()=>{this.onOpen();}}>More</Button>
</View>
);
}
}
そして今、私は私がボタンを押したときに私にエラーを与える、これを試してみました:
エラー:
Unhandled JS Exception: _this.setState is not a function. (In '_this.setState({ visible: true })', '_this.setState' is undefined)
コード:
let onOpen =() => {
this.setState({visible:true});
console.log(this.state.visible);
}
export default class HomeScreen extends Component {
constructor(props) {
super(props);
this.state = {
visible: false
}
this.onOpen = onOpen.bind(this);
}
render() {
return (
<View style={styles.container}>
<Button onPress={()=>{this.onOpen();}}>More</Button>
</View>
);
}
}
を
エクスポートする機能について具体的に教えてください。 –
私はあなたの意見を持っていましたが、クラスとOOPパターンの詳細と 'this'をどう使うべきですか –