简体   繁体   English

React - 通过道具在孩子中设置状态

[英]React - setState in child through props

I'm relatively new to React and am trying to set the state of an object inside the child component( SportTile ) for the onclick event.我对 React 比较陌生,我正在尝试在 onclick 事件的子组件( SportTile )内设置 object 的state I'm comfortable updating the state of a single variable by using individual useState() hooks but, that is a cumbersome task when you have a lot of variables.我很乐意通过使用单独的useState()挂钩来更新单个变量的 state,但是,当您有很多变量时,这是一项繁琐的任务。 So I used an object named: isClicked which stores a boolean value for various sports on whether they are selected by the user or not.所以我使用了一个名为: isClicked的 object,它存储了各种运动的 boolean 值,以确定它们是否被用户选择。

The functionality that I'm looking for is, whenever a SportTile gets clicked on, its isClicked["sportname"] value is set to true and the rest are set to false.我正在寻找的功能是,每当单击SportTile时,它的isClicked["sportname"]值设置为 true,并且 rest 设置为 false。 Only one sport can be true at once.一次只有一项运动是真实的。 Upon console logging the isClicked object in the onclick() function, I got the desired values but they weren't updated in the Parent component's( Booking ) h1 tag在控制台在onclick() function 中记录isClicked object 后,我得到了所需的值,但它们没有在父组件的 ( Booking ) h1标记中更新

 import React from 'react'; import SportTile from '../SportTile'; import './booking.css'; import { useState } from 'react'; import SportsSoccerRoundedIcon from '@mui/icons-material/SportsSoccerRounded'; import SportsBasketballRoundedIcon from '@mui/icons-material/SportsBasketballRounded'; import SportsVolleyballIcon from '@mui/icons-material/SportsVolleyball'; import SportsTennisIcon from '@mui/icons-material/SportsTennis'; const Booking = () => { const [isClicked, setIsClicked] = useState({ Football: false, Basketball: false, Volleyball: false, Tennis: false, }); return ( <div className='booking'> <div className='booking__body'> <div className='booking__left'> <h1>SELECT SPORT</h1> <SportTile sportname={'Football'} icon={<SportsSoccerRoundedIcon />} clicked={setIsClicked} isClicked={isClicked} // test={setTestclicked} // istestClicked={istestClicked} /> <SportTile sportname={'Basketball'} icon={<SportsBasketballRoundedIcon />} clicked={setIsClicked} isClicked={isClicked} /> <SportTile sportname={'Volleyball'} icon={<SportsVolleyballIcon />} clicked={setIsClicked} isClicked={isClicked} /> <SportTile sportname={'Tennis'} icon={<SportsTennisIcon />} clicked={setIsClicked} isClicked={isClicked} /> <h1>Football: {isClicked.Football.toString()} </h1> <h1>Basketball: {isClicked.Basketball.toString()} </h1> <h1>Volleyball: {isClicked.Volleyball.toString()} </h1> <h1>Tennis: {isClicked.Tennis.toString()} </h1> </div> <div className='booking__right'>Right Side</div> </div> </div> ); }; export default Booking;

 import { Button } from '@mui/material'; import React from 'react'; import './sportTile.css'; const SportTile = ({ sportname, icon, clicked, isClicked, }) => { function onclick() { Object.keys(isClicked).map((key) => { if (key == sportname) { isClicked[key] = true; } else { isClicked[key] = false; } }); clicked(isClicked); console.log(isClicked); } return ( <Button className='sportname__button' onClick={onclick}> <div className='sportname__buttonColumn'> {icon} {sportname} </div> </Button> ); }; export default SportTile;

Maybe I'm missing the obvious but would appreciate anyone who could point me in the right direction也许我错过了显而易见的事情,但会感谢任何能指出我正确方向的人

You should never pass the original setState method.你永远不应该传递原始的 setState 方法。 create a new method in the Booking component:在 Booking 组件中创建一个新方法:

const setIsClickedWrapper = (sportKey) = {
   setIsClicked((isClicked) => Object.keys(isClicked).map((key) => sportKey === key)
} 

and in the SportTile component just call:在 SportTile 组件中调用:

const onclick = () => { setIsClickedWrapper(sportname) }     

but I think it will be better if isClicked will be just a string of the current clicked sport key and then it's enough:但我认为如果 isClicked 只是当前单击的运动键的字符串会更好,然后就足够了:

const setIsClickedWrapper = (sportKey) = {
   setIsClicked(sportKey)
} 

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

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