【问题标题】:How to display items in React using array.map, useState, useEffect at random with onClick?如何使用array.map,useState,useEffect在React中随机显示项目和onClick?
【发布时间】:2020-12-31 14:53:22
【问题描述】:

我正在尝试制作一个名称生成器组件,该组件将在用户单击按钮时从数组中显示具有相应含义的名称。我正在使用 useState 来设置初始状态以及随机循环数组,以及 useEffect (尽管不确定是否有必要)。我更喜欢使用 array.map() 方法进行随机化。组件代码如下:

import React, { useState, useEffect } from "react";

const GeneratorNew = () => {
  const startingName = [{ id: 0, value: "Update Name!" }];

  const nameList = [
      { id: 1, name: 'Astghik', meaning: 'little star' }, 
      { id: 2, name: 'Tagouhi', meaning: 'Queen' }, 
      { id: 3, name: 'Lusine', meaning: 'Moon' }]
  ;

  const [stateOptions, setStateValues] = useState(startingName);
  // startingName.push(...nameList);

  useEffect(() => {
    setStateValues(nameList);
  }, []);

  return (
    <div>
      <h1>{nameList.name}</h1>
      <p>{nameList.meaning}</p>
      <button>
        {stateOptions.map((nameList, index) => (
          <option key={nameList.id}>{nameList.value}</option>
        ))}
      </button>
    </div>
  );
};

export default GeneratorNew;

【问题讨论】:

    标签: javascript arrays reactjs use-state


    【解决方案1】:
    import React, { useState, useEffect } from "react";
    
    const GeneratorNew = () => {
      const startingName = [{ id: 0, value: "Update Name!" }];
    
      const nameList = [
          { id: 1, name: 'Astghik', meaning: 'little star' }, 
          { id: 2, name: 'Tagouhi', meaning: 'Queen' }, 
          { id: 3, name: 'Lusine', meaning: 'Moon' }]
      ;
    
      const [stateOptions, setStateValues] = useState(startingName);
    
    
      const handleOnClick = () => {
         setStateValues(nameList[Math.floor(Math.random() * (nameList.length - 1))])
      }
    
      return (
        <div>
          <h1>{nameList.name}</h1>
          <p>{nameList.meaning}</p>
          <button onClick={() => handleOnClick()}>
            {stateOptions.map((nameList, index) => (
              <option key={nameList.id}>{nameList.value}</option>
            ))}
          </button>
        </div>
      );
    };
    
    export default GeneratorNew;
    

    【讨论】:

      【解决方案2】:

      这里不需要useEffect 和useState 只要有索引就足够了。 试试这样的 sn-p。在每个按钮单击时生成随机索引。

      const GeneratorNew = () => {
        const startingName = [{ id: 0, meaning: "Update Name!" }];
      
        const nameList = [
            { id: 1, name: 'Astghik', meaning: 'little star' }, 
            { id: 2, name: 'Tagouhi', meaning: 'Queen' }, 
            { id: 3, name: 'Lusine', meaning: 'Moon' }]
        ;
      
        const [randomIndex, setRandomIndex] = React.useState(-1);
      
        const data = randomIndex !== -1 ? nameList[randomIndex] : startingName[0];
      
        return (
          <div>
            <h1>{data.name}</h1>
            <p>{data.meaning}</p>
            <button onClick={() => setRandomIndex(Math.floor(Math.random() * nameList.length))}>
              Get Random 
            </button>
          </div>
        );
      };
      
      ReactDOM.render(<GeneratorNew /> , document.querySelector('#app'))
      <script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
      <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
      
      <div id="app"> </div> 

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-14
        • 2018-08-01
        • 1970-01-01
        • 2022-01-12
        • 2020-03-23
        • 2021-07-18
        • 2020-12-04
        • 1970-01-01
        相关资源
        最近更新 更多