小编Ham*_*lih的帖子

这个价差操作符是如何、为什么工作的

这是代码

funtion App(){
  const [Cards, setCards] = useState([
    {id: 0 , name: 'Harry Potter'},
    {id: 1 , name: 'Hermonie Granger'},
    {id: 2 , name: 'Ron Weasly'},])

  const shuffle = (arr)=>{
    // just shuffle the arr then return it.
  }
    
   return (
    <div className="App">
      
      <div>{Cards[0].name}</div>
      
      <button onClick={() => {
        setCards([...shuffle(Cards)])
        console.log(Cards)
        
        }}>Testing</button>
    </div>
  );

}
Run Code Online (Sandbox Code Playgroud)

为什么这个有效setCards([...shuffle(Cards)]) ,而不是这个setCards(shuffle(Cards))

即使我不使用展开运算符,它也会洗牌(请参阅控制台),但不会在页面中显示它。

:) ;) ....

spread reactjs react-hooks

1
推荐指数
1
解决办法
69
查看次数

标签 统计

react-hooks ×1

reactjs ×1

spread ×1