简体   繁体   English

在事件React Native上更改文本

[英]Change text on event React Native

I want to change some text when an event happens. 我想在事件发生时更改一些文本。 But I don't know how to do it. 但是我不知道该怎么做。

I saw some tutorials where people are creating a new component. 我看到了一些教程,人们可以在其中创建新组件。 But is this the way? 但这是这样吗? Because it is one sentence of text that needs to be replaced. 因为这是一句话,需要替换。

This is my code, I have some text that is displayed. 这是我的代码,显示一些文本。 And underneath I am displaying an agenda. 在下面,我正在显示一个议程。 When I press an day (onDayPress) I want to replace the text inside the text key with the text printed in my console.log. 当我按一天(onDayPress)时,我想将文本键内的文本替换为console.log中打印的文本。

export default class App extends React.Component {
  render() {
    return (

      <View style={styles.container}>
          <Text style={styles.headline}>Januari 2018</Text>

          <Agenda
              onDayPress={(day)=>{
                  console.log(LocaleConfig.locales['nl'].monthNames[day.month - 1] + " " + day.year)
              />
      </View>
    );
  }
}

Can someone help me or point me in the right direction. 有人可以帮助我或为我指明正确的方向。 Thank you. 谢谢。

You should use state to store the text. 您应该使用状态来存储文本。 Initialize your state with the default value, then when user click on Agenda, change that text and render the same. 使用默认值初始化您的状态,然后在用户单击“议程”时,更改该文本并进行渲染。 Hope following code will help. 希望以下代码会有所帮助。

export default class App extends React.Component {
 constructor(args) {
   super(args);
   this.state = {
     text: 'Januari 2018'
   };
 }

 render() {
  const { text } = this.state;
  return (
  <View style={styles.container}>
      <Text style={styles.headline}>{text}</Text>
      <Agenda
          onDayPress={(day)=>{
              this.setState({
               text: LocaleConfig.locales['nl'].monthNames[day.month - 1] + " " + day.year
          });
      />
  </View>
  );
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM