简体   繁体   English

如何使用条件和 React Hooks 设置随机初始状态?

[英]How can I set a randomized initial state using a conditional and React Hooks?

This example simulates a coin toss.此示例模拟抛硬币。
I would like to randomize the initial state of the coin.我想随机化硬币的初始状态。 The first time the page is loaded it could show heads.第一次加载页面时,它可能会显示头像。 The next time, tails.下一次,尾巴。 To do so, I am using a random number generated by the Math method and a conditional to determine the face of the coin based on the random number (even number shows heads, odd shows tails).为此,我使用 Math 方法生成的随机数和一个条件来根据随机数确定硬币的正面(偶数表示正面,奇数表示反面)。
The value needs to be displayed upon the initial render.该值需要在初始渲染时显示。

I also want to store the value in state using React's Hooks to use later in my app.我还想使用 React 的 Hooks 将值存储在状态中,以便稍后在我的应用程序中使用。 How can I set a randomized initial state value using hooks?如何使用钩子设置随机初始状态值?

Here is the code that I am working with.这是我正在使用的代码。 It currently does not function and I'm not sure what I am doing incorrectly to achieve my goal:它目前不起作用,我不确定我在做什么不正确来实现我的目标:

import React, { useState } from 'react';
import './App.css';

function App() {

  const [coin, setCoin] = useState(randomizePlayer())

  const randomizePlayer = () => {
    let number = Math.floor(Math.random() * 10) + 1
    return (number % 2 === 0) ? setCoin('Heads') : setCoin('Tails')
  }

    return (
      <div className="App">
        The coin is showing {coin}
      </div>
    );
  }

export default App;

I'm brand new to the hooks API and am using this exercise to learn.我是 hooks API 的新手,正在使用这个练习来学习。

Any help is appreciated!任何帮助表示赞赏!

I would rewrite this code as follows:我将重写这段代码如下:

const randomizePlayer = () => {
  const number = Math.floor(Math.random() * 10) + 1
  return (number % 2 === 0) ? 'Heads' : 'Tails'
}

function App() {
  const [coin, setCoin] = useState(randomizePlayer())

  return (
    <div className="App">
      The coin is showing {coin}
    </div>
  );
}

randomizePlayer does not need to call setState ; randomizePlayer不需要调用setState let it just return a value.让它只返回一个值。 useState can use that value as its initial state. useState可以使用该值作为其初始状态。

Also to clarify: useState gets called just once.还要澄清一下: useState只被调用一次。 If you want coin to have a different value, the way to do that with this setup is to remount the component (reload the page).如果您希望coin具有不同的值,使用此设置的方法是重新安装组件(重新加载页面)。

State hooks docs状态挂钩文档

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

相关问题 React Hooks - 将 state 设置为初始 state - React Hooks - set state to initial state 如何在 React Hooks 中修改 prop 并将其设置为初始状态? - How to modify prop and set it to initial state in React Hooks? 在 React Hooks 中破坏后如何设置 state 值? - How can i set state value after destructing in React Hooks? 无法使用钩子在 React 中设置状态 - Can't set state in React using hooks 如何从 API 数据设置初始 React 状态? - How can I set initial React state from API data? 如何使用 React hooks 在 localStorage 中存储初始切换状态 - How to store initial toggle state in localStorage using React hooks 如何使用反应钩子设置状态数组 - how to set state array using react hooks 如何使用 react-navigation v5 设置初始状态以将屏幕包含为历史记录? - How can I set the initial state using react-navigation v5 to include screens as history? 如何在反应原生中使用选择器从 redux state 设置 usestate 的初始值? - how can i set initial value for usestate from redux state using selector in react native? 如何在反应 class 组件中设置另一个 state 以将数据从初始 state 传递到 - How can I set another state to pass data to from the initial state in a react class component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM