简体   繁体   English

如何使用 react/react native 在单击时删除或隐藏多个组件

[英]How to delete or hide multiple components on click with react/react native

https://res.cloudinary.com/catify/image/upload/v1588704903/hcnqjp7okfykkb3az2v3.jpg https://res.cloudinary.com/catify/image/upload/v1588704903/hcnqjp7okfykkb3az2v3.jpg

Hello im trying to create a proyect of a guessing game, i have multiple components of letters as show in the image, some letters are needed for the answer and some are not, i need a button that when i click it it removes or hides the components that are not needed for the answer, how can i do this with react or react native?您好,我正在尝试创建一个猜谜游戏的项目,我有多个字母组件,如图所示,答案需要一些字母,而有些则不需要,我需要一个按钮,当我点击它时,它会删除或隐藏答案不需要的组件,我如何使用 react 或 react native 来做到这一点?

Im saving the letters in a array and then rendering them using Map with a custom component that is styled to look like the photo, im doing it in react native but i think it should be the same in react, any help is welcome, thanks.我将字母保存在一个数组中,然后使用 Map 和一个自定义组件来渲染它们,该组件的样式看起来像照片,我在 react native 中这样做,但我认为在 react 中应该是相同的,欢迎任何帮助,谢谢。

  return (
    <Animated.View style={{flex: 1}}>
      {Letters.forEach(element => {
        <LetterCard letter={element} />;
      })}

      <Button
        title="eliminar"
        onPress={() => {
          eliminar;
        }}
      />
    </Animated.View>
  );

You probably need a list in state or somewhere that holds which letters are needed and which aren't, as well as a boolean to determine if you are showing all letters or just your needed letters.您可能需要 state 中的列表或包含哪些字母需要哪些不需要的地方,以及 boolean 来确定您是显示所有字母还是只显示您需要的字母。

Your button which toggles to show/hide the unneeded letters would simply toggle the neededOnly state.切换显示/隐藏不需要的字母的按钮将简单地切换所需的 state。

this.state={        
    neededLetters = [],   //array of needed letters
    neededOnly = false,
}

{neededOnly ? 
neededLetters.forEach(element => {
    <LetterCard letter={element} />;
}) : 
Letters.forEach(element => {
    <LetterCard letter={element} />;
})}


<Button
    title="eliminate"
    onPress={() => {
      this.setState(prevState => ({
      neededOnly: !prevState.neededOnly
    }));
/>

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

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