在React Native中,如何在按钮按下时更改文本值?

16

我是一位iOS开发者,目前正在开发一个实验性的React Native应用。

以下是展示在屏幕上的包含按钮和示例文本的代码。

import React from 'react';
import { StyleSheet, Text, View , Button } from 'react-native';

export default class App extends React.Component {
  constructor() {
    super();
    this.state = {sampleText: 'Initial Text'};
  }

  changeTextValue = () => {
    this.setState({sampleText: 'Changed Text'});
  }

  _onPressButton() {
    <Text onPress = {this.changeTextValue}>
      {this.state.sampleText}
    </Text>
  }

  render() {
    return (
      <View style={styles.container}>
        <Text onPress = {this.changeTextValue}>
          {this.state.sampleText}
        </Text>

        <View style={styles.buttonContainer}>
          <Button
            onPress={this._onPressButton}
            title="Change Text!"
            color="#00ced1"
          />
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#f5deb3',
    alignItems: 'center',
    justifyContent: 'center',
  },
  buttonContainer: {}
});

以上代码显示文本和按钮。

但是,当我点击按钮时,应该展示的新文本却导致了应用崩溃。

我刚接触React Native,请指导我如何解决此错误。

8个回答

25

您可以使用一个状态来保存默认文本,然后在按下时更新该状态。

import React, { Component } from 'react'
import { View, Text, Button } from 'react-native'

export default class App extends Component {
  state = {
    textValue: 'Change me'
  }

  onPress = () => {
    this.setState({
      textValue: 'THE NEW TEXT GOES HERE'
    })
  }

  render() {
    return (
      <View style={{paddingTop: 25}}>
        <Text>{this.state.textValue}</Text>
        <Button title="Change Text" onPress={this.onPress} />
      </View>
    )
  }
}

更改"function App()"为"class App extends Component"时出现了一堆错误。 - undefined

1
您可以使用状态来动态更改文本。
import React, {Component} from 'react';
import {Text, Button, View} from 'react-native';

export default class App extends Component{
constructor(){
    super();
    this.state = {
    textValue: 'Temporary text'
    }
    this.onPressButton= this.onPressButton.bind(this);
}

onPressButton() {
    this.setState({
        textValue: 'Text has been changed'
    })
}

render(){
    return(

<View style={{paddingTop: 20}}>
  <Text style={{color: 'red',fontSize:20}}> {this.state.textValue} </Text>
  <Button title= 'Change Text' onPress= {this.onPressButton}/>
</View>

   );
 }
}

1
用hooks实现:

import React, {useState} from "react";
import {Button, Text, View} from "react-native";

const App = () => {
  const [text, setText] = useState("Initial text");

  const onPressHandler = event => setText("Changed text");

  return (
    <View>
      <Text>{text}</Text>
      <Button title="Change Text" onPress={onPressHandler} />
    </View>
  );
};

0

这是因为你的 onPress 函数有点奇怪,你想在按下时调用一个动作,而不是 jsx 元素。你应该将你的 changeTextValue 传递到按钮的 onPress 中。


你的是 changeTextValue,不是 you're。 - ahmetkilinc

0

你最好确定一下 _onPressButton() 函数在做什么。你可以在这个函数中简单地使用 setState(),不做其他操作,这可以帮助你解决问题。如果你想渲染新的内容,你需要添加 return() 并将 Text 包装起来。


0

将我的文本设置在状态方法中,然后在按下按钮时更新状态,然后像这样在文本中设置:

<Text>
    {this.state.myText}
</Text>

0

您可以使用此方法在单击按钮时更新值

class App extends React.Component {
   constructor() {
     super();
     this.state = { val: 0 }
     this.update = this.update.bind(this)
   }
   update() {
     this.setState({ val: this.state.val + 1 })
   }
   render() {
     console.log('render');
     return <button onClick={this.update}>{this.state.val}</button>
   }
}

0
import React, { useState } from "react";
import { View, Text } from "react-native";

const App = () => {
  const [value, setValue] = useState("Mohd Sher Khan");

  const hellod = () => {
    setValue("value changed");
    
  };

  return (
    <View>
      <Text onPress={hellod}>{value}</Text>
    </View>
  );
};

export default App;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接