简体   繁体   English

为什么反应文本组件不显示对我的数组 state 的更新

[英]Why is the react text component not displaying updates to my array state

I am trying to familiarize myself with React Native.我正在尝试熟悉 React Native。 At the moment I am working on an app but came across an issue when trying to display changes to the individual elements of an array.目前我正在开发一个应用程序,但在尝试显示对数组的各个元素的更改时遇到了一个问题。 For example:例如:

function MyApp() {
    const [array, setArray] = useState([1,2,3]);

    const onPress = () => {
        let temp = [3,2,1];
        setArray(temp);
    }
    
    return(
        <View>
            <TouchableHighlight onPress={onPress}> 
                 <View>
                     <Text>{array[0]}</Text>
                 </View>
            </TouchableHighlight>
        </View>
    );
}

With the above code, I expect '1' to be displayed in the text component and to change to '3' upon being pressed.使用上面的代码,我希望在文本组件中显示“1”并在按下时更改为“3”。 console.log shows the state being changed but what is being displayed in the text component inside the actual app never updates. console.log 显示 state 正在更改,但实际应用程序内的文本组件中显示的内容永远不会更新。 I then tried this using individual integer states like so:然后我使用单独的 integer 状态尝试了这个,如下所示:

const [int, setInt] = useState(0);

const onPress = () => {
    setInt(1);
}

Using an integer state such as the one above works totally fine as expected.使用 integer state (如上述)完全可以按预期工作。 Can anyone show me what I am doing wrong with my array state?谁能告诉我我的阵列 state 做错了什么? Thank you.谢谢你。

Your code looks perfect and should work without any issue.您的代码看起来很完美,应该可以正常工作。

Here is the slightly modified example where the first element is generated randomly and is being shown properly in the Text component.这是稍微修改的示例,其中第一个元素是随机生成的,并且在Text组件中正确显示。

Working Example: Expo Snack工作示例: Expo 小吃

在此处输入图像描述

import React, { useState } from 'react';
import { Text, View, StyleSheet, TouchableHighlight } from 'react-native';
import Constants from 'expo-constants';

export default function MyApp() {
  const [array, setArray] = useState([1, 2, 3]);

  const onPress = () => {
    let temp = [Math.floor(Math.random() * 10), 2, 1];
    setArray(temp);
  };

  return (
    <View style={styles.container}>
      <TouchableHighlight onPress={onPress}>
        <View style={styles.btn}>
          <Text
            style={{ alignSelf: 'center', fontSize: 28, fontWeight: 'bold' }}>
            {array[0]}
          </Text>
        </View>
      </TouchableHighlight>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  btn: {
    width: 100,
    height: 100,
    borderColor: 'purple',
    borderWidth: 5,
    justifyContent: 'center',
  },
});

Your code looks to be fine, I tried the below following your code and can see that state and UI getting updated successfully when the button is being clicked.您的代码看起来不错,我按照您的代码尝试了以下操作,可以看到 state 和 UI 在单击按钮时成功更新。

Could you please check if your event handler function onPress is getting called, and if you are getting any error in the console when you click on it.您能否检查一下您的事件处理程序 function onPress是否被调用,以及单击控制台时是否出现任何错误。

function App() {
  const [array, setArray] = React.useState([1, 2, 3]);

  const handleBtnClick = () => {
    const temp = [3, 2, 1];
    setArray(temp);
  };

  return (
    <React.Fragment>
      <button onClick={handleBtnClick}>Click</button>
      {array.map((el) => (
        <p>{el}</p>
      ))}
      <hr />
      {array[0]}
    </React.Fragment>
   );
}

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

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