【发布时间】: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