【问题标题】:How to show/hide an item of array.map() based on onclick inside component如何基于组件内部的 onclick 显示/隐藏 array.map() 项
【发布时间】:2020-07-24 17:57:10
【问题描述】:

我想根据 P_Gamestate 属性显示/隐藏 JSX 的一部分。如果这正在进行中,我们将只显示我们单击的组件。否则其他一切都应该隐藏起来。这很困难,因为.map() 指的是播放列表中的所有元素。我怎样才能做到这一点? 目前它会隐藏我点击的组件,但其他所有内容都将保持显示。

challenge.js:

export default function Challenges()
{
    const [P_gameState, P_setGameState] = useState(GAME_STATE.BEFORE);

    useEffect(() => {
        async function getPlaylists() {
           let querySnapshot =  await firestoreRef
            .collection('challenge_test')
            .get();
            querySnapshot.forEach(function(data) {
                playlist.push([useful data]);
            });
        }
        getPlaylists();

    },[]);
    return(
        <div>
            <header>
                {console.log("rendering component...")}
                {playlist.map((row)=> (

                        <CardComponent key={row[0]}
                                       name={row[0]}
                                       image={row[1]}
                                       creator={row[2]}
                                       challengeID={row[3]}
                                       P_gameState={P_gameState}
                        />
                )
                )
}         
        </div>
    );
}

卡片组件.js

function CardComponent(props) {
    const [gameState, setGameState] = useState(GAME_STATE.BEFORE);

    const onClick = () => {
        console.log(props.history);
        props.history.push("/playchallenge");
    };
    return (
        <div>
        { gameState === GAME_STATE.BEFORE &&
        <div className='card-component'>
            <Button onClick={() => setGameState(GAME_STATE.IN_PROGRESS)}>
                <img
                    className="playlist-image"
                    src={props.image}
                    alt={props.name} />
            </Button>
            <h3 className='playlist-title'>{props.name}</h3>
            <h4 className='playlist-creator-text'>Created by {props.creator}</h4>
        </div>
    }
     { gameState === GAME_STATE.IN_PROGRESS &&   <FetchData />}
</div>

    )
}

【问题讨论】:

    标签: javascript reactjs firebase react-router jsx


    【解决方案1】:

    如果你想在 JSX 中做条件渲染,你有多种方法:

    1. map 函数回调中进行分支
    array.map(item => {
      if (!condition) {
        return null;
      }
      return <Component {...props} />;
    });
    
    1. 在组件渲染函数内部进行分支(类似于方法一)
    2. map 之前调用array.filter(这将每次都创建一个过滤后数组的新副本)
    3. 内联 If 与逻辑 && 运算符
    array.map(item => condition && <Component {...props} />);
    
    1. 内联 If 和条件(三元)运算符
    array.map(item => condition? <Component {...props} />: <SomethingElse />);
    

    ============

    如果您希望内部组件中发生某些事件来更新条件,请使用事件道具

    <CardComponent onClick={() => { /* do something with your game state */ }} />
    

    【讨论】:

    • 谢谢你。如何识别我的 array.map() 中的特定数组?它实际上是一个数组数组。
    • 我认为你想更新内部组件点击事件的条件,所以我更新了我的答案。
    • 谢谢,我在这样做的时候遇到了一个外观问题。我希望我的组件并排列出,但是由于我在 jsx 中返回了每个组件,因此它们都一个一个地列在另一个之上。你有什么建议吗?
    • 请看这个供参考:codepen.io/tonioshikanlu/pen/ZEbGEoN
    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    相关资源
    最近更新 更多