【问题标题】:Is there a way to use dynamic variables in React or React Native有没有办法在 React 或 React Native 中使用动态变量
【发布时间】:2021-11-18 15:29:43
【问题描述】:

GUEST_1 是一个变量。我需要能够动态地改变最后的数字,它代表我的变量。有什么办法可以做到这一点:目前我没有定义 GUEST_gestNumber。我从这个例子 GUEST_1 GUEST_2 GUEST_3 中得到了不同的文本

  const guest = (guestNumber) => {
    return (
      <View>
        <Text>{GUEST_guestNumber}</Text>
      </View>
    );
  };

【问题讨论】:

  • 我建议你做一些基本的编程教程

标签: reactjs react-native


【解决方案1】:

这是你想要的吗?

  const guest = (guestNumber) => {
    return (
      <View>
        <Text>{'GUEST_' + guestNumber}</Text>
      </View>
    );
  };

如果不是,你的问题很不清楚。

【讨论】:

  • 我试过这个。我得到的只是 GUEST_1 或 GUEST_2。我希望从中获取文本,例如 GUEST_1 是一个变量,应该显示 Jerry 而 GUEST_2 应该显示 Marry
  • 那么您可以使用对变量父级的引用。像this['GUEST_1'] 这样的东西在你的情况下是this['GUEST_' + guestNumber]
猜你喜欢
  • 2021-06-28
  • 2015-12-21
  • 1970-01-01
  • 2020-10-30
  • 2021-09-04
  • 1970-01-01
  • 2020-05-20
  • 2019-06-03
  • 2018-05-06
相关资源
最近更新 更多