简体   繁体   English

如果 React Native Function 中的 prop 更改,则渲染

[英]Render if prop changes in React Native Function

is it possible, in a React Native Function, to render the "return" at changes?是否有可能在 React Native Function 中呈现更改时的“返回”? What I try:我尝试什么:

I have a Function - this Function gets an specific array out of another Function - on Button Press I generate a new Index - now what I want is to re-render the View to display the array element with the new Index:我有一个 Function - 这个 Function 从另一个 Function 中获取一个特定的数组 - 在按钮按下我生成一个新的索引 - 现在我想用新的索引显示数组元素:

const generateNewIndex = function (item) {
  return Math.floor(Math.random() * item.length);
};
const PositionItem = ({ arr, position, navigation }) => {
  let { name, beschreibung, arten, rooms, isStarred } = position;
  let testArray = arr;

  let i = 0;

  return (
    <View style={styles.posContainer}>
      <View style={styles.titles}>
        <Text style={styles.title}>{name}</Text>
        <Text style={styles.subtitle}>{beschreibung}</Text>
        <Text style={styles.title}>{testArray[i].name}</Text>
      </View>

      <View style={styles.buttonsContainer}>
        <StyledButton
          type="primary"
          content={"Next Random Position"}
          onPress={() => {
            console.warn("Pressed");
            i = generateNewIndex(testArray);
          }}
        />
      </View>
    </View>
  );
};

export default PositionItem;

Thanks in advance!提前致谢!

I have found a way which is Working.我找到了一种有效的方法。 If anyone wonders in the Future what I did:如果有人想知道未来我做了什么:

add a Use State component:添加一个使用 State 组件:

const [count, setCount] = useState(0);

onPress on Button increase the Count: onPress on Button 增加计数:

onPress={() => {
            i = generateNewIndex(array);
            setCount((prevCount) => prevCount + 1);
          }}

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

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