简体   繁体   English

当多个组件在 React 中使用 state 时,如何仅使用 state 渲染单个组件?

[英]How to only render an individual component with state, when multiple components use that state in React?

I'm currently using state to display/hide an icon in my SwipeRow , but when I swipe one row, the icon becomes visible on all the rows, because the icons all reference that same state, so my question was how do I display the icon only on the row swiped?我目前正在使用 state 在我的SwipeRow中显示/隐藏一个图标,但是当我滑动一行时,该图标在所有行上都可见,因为这些图标都引用了相同的 state,所以我的问题是如何显示图标仅在刷过的行上?

I was thinking maybe I can utilize index somehow in .map() , like wrap StyledContainer with a ternary using index , I'm not sure.我在想也许我可以在.map()中以某种方式利用index ,比如使用indexStyledContainer包装成三元组,我不确定。 Any ideas on how to achieve this?关于如何实现这一目标的任何想法? If you have a method without using state, that's also fine.如果您有不使用 state 的方法,那也没关系。

const foobar = content.map((object, index) => {
  return (
    <SwipeRow
       onRowOpen={() => setSwiped(true)}
       onRowClose={() => setSwiped(false)}
    >
          <StyledContainer swiped={isSwiped}>  <-- This is where it's displayed/hidden
              <Icon />
          </StyledContainer>
            
    </SwipeRow>
 )})

You will have to maintain a state for all Swipe rows.您必须为所有 Swipe 行维护 state。 Maybe a map or an object.也许是 map 或 object。 Something like this.像这样的东西。

    const [swiped, setSwiped] = React.useState({});
    const content = content.map((object, index) => {
      const id = object.id;
      return (
        <SwipeRow
           onRowOpen={() => setSwiped((oldSwipe) => { oldSwipe.id = true; return oldSwipe;})}
           onRowClose={() => setSwiped((oldSwipe) => { oldSwipe.id = false; return oldSwipe;})}
        >
              <StyledContainer swiped={swiped[id]}>  <----- This is where it's displayed/hidden
                  <Icon />
              </StyledContainer>
                
        </SwipeRow>
     )})

暂无
暂无

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

相关问题 React - 当父状态组件发生更改时,如何强制子组件重新呈现? - React - How do i force child components to re render when parent state component changes? React:如何将状态值传递给另一个组件并使用它来呈现组件 - React: How to pass State value to another component and use it to render a component 如何使用 state 在 React 中渲染表单组件? - How can I use state to render form components in React? 如何仅在设置 state 后渲染反应组件? - How do I render a react component only after a state is set? 如何多次渲染React组件,但是每次都具有更新的State? - How to render a React component multiple times but each time with an updated State? 更改单个反应组件的 state - changing state of individual react component React 在重新渲染父组件时如何重用子组件/保留子组件的 state? - How does React re-use child components / keep the state of child components when re-rendering the parent component? 渲染组件时如何在我的过滤器函数中使用this.state? - How to use this.state in my filter function when render component? 如何在 React 组件的第一次渲染时不改变状态? - How to not change the state at the first render of React component? 反应:具有相同事件处理程序的多个组件:尝试仅更改被单击的组件而不是所有其他组件的 state - React: multiple components with the same event handler: trying to change the state of only the component which was clicked and not of all the others
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM