我有按钮,我想在按下按钮时提醒随机文本。我该怎么做?
这是我到目前为止所尝试过的。当它发出警报时,它什么也没显示。
constructor(props) {
super(props)
this.state = {
notes: ['me', 'hi', 'you'],
selectedNote: null,
clicked: false
}
}
handleClick = () => {
this.setState({
clicked: true,
selectedNote: this.state.notes[Math.floor(Math.random() *
this.state.notes.length)]
})
}
onPressButton() {
Alert.alert(this.handleClick)
}
render() {
return (
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button
onPress={this.onPressButton}
title="You"
/>
</View>
</View>
);
}
}
每当我按下按钮时,我需要提醒我随机指定的文本
最佳答案
在这种情况下,您应该提醒 this.handleClick
函数,而应该提醒 selectedNote
。
onPressButton = () => {
const { notes } = this.state;
const selectedNote = notes[Math.floor(Math.random() * notes.length)];
this.setState({
clicked: true,
selectedNote
});
Alert.alert(selectedNote);
}
关于javascript - 如何在 native react 中单击按钮时提醒随机文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57926041/