如何使用reactjs在子组件中将函数用作props

bvjveswy  于 2022-11-04  发布在  React
关注(0)|答案(3)|浏览(165)

我正在尝试用react做一个简单的tik-tak-toe游戏,但是我的代码有一些问题。
这是父组件:

const Wrapper = () => {

   const [cases, setCases] = useState(Array(9).fill(null));
   const [isX, setIsX] = useState(true);

   const handleClick = (i) => {
        cases[i] = isX ? 'X' : 'O';
        setCases(cases);
        setIsX(!isX);
        console.log(cases)
   }

    return ( 
        <div className='ttt-wrapper'>
            <div className='ttt-3-cols'>
                <Case value={cases[0]} onClick={()=> handleClick(0)}/>
                <Case value={cases[1]} onClick={()=> handleClick(1)}/>
                <Case value={cases[2]} onClick={()=> handleClick(2)}/>
            </div>
            <div className='ttt-3-cols'>
                <Case value={cases[3]} onClick={()=> handleClick(3)}/>
                <Case value={cases[4]} onClick={()=> handleClick(4)}/>
                <Case value={cases[5]} onClick={()=> handleClick(5)}/>
            </div>
            <div className='ttt-3-cols'>
                <Case value={cases[6]} onClick={()=> handleClick(6)}/>
                <Case value={cases[7]} onClick={()=> handleClick(7)}/>
                <Case value={cases[8]} onClick={()=> handleClick(8)}/>
            </div>
        </div>
    )
}

这是子组件:

const Case = ({ value, handleClick }) => {
   console.log(value)
  return (
    <button className='ttt-case' onClick={handleClick} >
        { value }
     </button>
  )
}

如何使用里面的handleClick函数,以便使用click?

wwtsj6pe

wwtsj6pe1#

您可以执行handleClick,如下所示:

const Case = ({ value, handleClick }) => {
   console.log(value)
  return (
    <button className='ttt-case' onClick={()=>handleClick(value)} >
        { value }
     </button>
  )
}
8iwquhpp

8iwquhpp2#

const Wrapper = () => {

   const [cases, setCases] = useState(Array(9).fill(null));
   const [isX, setIsX] = useState(true);

   const handleClick = (i) => {
        const newCases = [...cases];  //making a copy
        newCases[i] = isX ? 'X' : 'O';   // I'm not sure what is the logic here tbh
        setCases(newCases);
        setIsX(x=>!x); // updater function
   }

    return ( 
        <div className='ttt-wrapper'>
            <div className='ttt-3-cols'>
                <Case value={cases[0]} handleClick={handleClick}/>
                <Case value={cases[1]} handleClick={handleClick}/>
                <Case value={cases[2]} handleClick={handleClick}/>
            </div>
            <div className='ttt-3-cols'>
                <Case value={cases[3]} handleClick={handleClick}/>
                <Case value={cases[4]} handleClick={handleClick}/>
                <Case value={cases[5]} handleClick={handleClick}/>
            </div>
            <div className='ttt-3-cols'>
                <Case value={cases[6]} handleClick={handleClick}/>
                <Case value={cases[7]} handleClick={handleClick}/>
                <Case value={cases[8]} handleClick={handleClick}/>
            </div>
        </div>
    )
}

const Case = ({ value, handleClick }) => {
   console.log(value)
  return (
    <button className='ttt-case' onClick={()=>handleClick(value)} >
        { value }
     </button>
  )
}
ltqd579y

ltqd579y3#

这是解决方案:

const Wrapper = () => {

   const [cases, setCases] = useState(Array(9).fill(null));
   const [isX, setIsX] = useState(true);

   const handleClick = (i) => {
        cases[i] = isX ? 'X' : 'O';
        setCases(cases);
        setIsX(!isX);
        console.log(cases)
   }

    return ( 
        <div className='ttt-wrapper'>
            <div className='ttt-3-cols'>
                <Case value={cases[0]} onClick={()=> handleClick(0)}/>
                <Case value={cases[1]} onClick={()=> handleClick(1)}/>
                <Case value={cases[2]} onClick={()=> handleClick(2)}/>
            </div>
            <div className='ttt-3-cols'>
                <Case value={cases[3]} onClick={()=> handleClick(3)}/>
                <Case value={cases[4]} onClick={()=> handleClick(4)}/>
                <Case value={cases[5]} onClick={()=> handleClick(5)}/>
            </div>
            <div className='ttt-3-cols'>
                <Case value={cases[6]} onClick={()=> handleClick(6)}/>
                <Case value={cases[7]} onClick={()=> handleClick(7)}/>
                <Case value={cases[8]} onClick={()=> handleClick(8)}/>
            </div>
        </div>
    )
}

外壳组件:

const Case = ({ value, onClick }) => {      return (
        <button className='ttt-case' onClick={onClick}>
            { value }
         </button>
      )
    }

相关问题