gpt4 book ai didi

javascript - React-Native 按钮 onPress setState

转载 作者:行者123 更新时间:2023-12-03 04:07:42 25 4
gpt4 key购买 nike

我已经查阅了 stackoverflow 上有关此问题的线程并观看了此 video关于如何制作可点击的按钮,但我仍然收到错误。

这是我的代码

class Button extends Component{
render(){
return(
<View>
<TouchableOpacity
onPress{() => { this.props.onPress() }}>
<Text>LOGIN</Text>
</TouchableOpacity>
</View>
)
}
};

export default class FirstPage extends Component{
constructor(props) {
super(props);
this.state = {clicked: false};
}

onTap = () => {
this.setState({
clicked: true
});
if (this.state.clicked){
return <SecondPage/>
}
}

render(){
return(
<View>
<Button onPress={() => { this.onTap() }}/>
</View>
);
}
}

但是,我收到此错误

<View>
<TouchableOpacity
onPress{() => { this.props.onPress() }}>
^
<Text>LOGIN</Text>
</TouchableOpacity>

我可以知道出了什么问题以及解决方案吗?

最佳答案

<TouchableOpacity
onPress={() => { this.props.onPress() }}>

<Text>LOGIN</Text>
</TouchableOpacity>

你忘记了“=”

继续;

export default class FirstPage extends Component{
constructor(props) {
super(props);
this.state = {clicked: false};
}

onTap = () => {
alert("点击");
this.setState({
clicked: true
});

}

render(){

return(
<View>
{this.state.clicked?
<SecondPage />
:
<Button onPress={() =>this.onTap()}/>
}
</View>
);
}
}
class SecondPage extends Component{
render(){
return(
<View>
<Text>第二页</Text>
</View>
);
}
}
class Button extends Component{
render(){
return(
<View>
<TouchableOpacity
onPress={() => { this.props.onPress() }}>
<Text>LOGIN</Text>
</TouchableOpacity>
</View>
)
}
};

AppRegistry.registerComponent('RNDemo', () => FirstPage);

关于javascript - React-Native 按钮 onPress setState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44473455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com