简体   繁体   English

如何在 React 中使用 array.map、useState、useEffect 和 onClick 随机显示项目?

[英]How to display items in React using array.map, useState, useEffect at random with onClick?

I'm trying to make a name generator component that will display a name with its corresponding meaning from an array when the user clicks a button.我正在尝试制作一个名称生成器组件,该组件将在用户单击按钮时从数组中显示具有相应含义的名称。 I'm using useState to set the initial state as well as to cycle through the array randomly, as well as useEffect (although not sure if its necessary).我正在使用 useState 设置初始 state 以及随机循环遍历数组以及 useEffect (尽管不确定是否有必要)。 I would prefer to use the array.map() method for randomizing.我更喜欢使用 array.map() 方法进行随机化。 Component code below:组件代码如下:

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;
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;

No need to useEffect here and useState only having the index is enough.这里不需要useEffect并且useState只有索引就足够了。 Try something like this snippet.试试这个片段。 Generate the random index on each button click.在每个按钮单击时生成随机索引。

 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>

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

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