简体   繁体   English

React.js:按下按钮以随机化数组

[英]Reactjs: Pressing button to randomize an array

Basically, I am trying to make a card program that would pick five cards out of 52 in random. 基本上,我正在尝试制作一个卡片程序,该程序将随机抽取52张卡片中的5张。 These cards must not repeat. 这些卡不得重复。 I have already figured out the randomizer through traditional javascript. 我已经通过传统的javascript找出了随机器。 However, I am using ReactJs to make a button which if pressed, would create a new set of five cards. 但是,我正在使用ReactJs创建一个按钮,如果按下该按钮,将创建一组新的五张卡。

class Reset extends React.Component {
  constructor(props) {
    super(props);
    this.state = {...};
  }

  handleClick() {...}

  render() {
    return <button onClick={this.handleClick}>{...}</button>;
  }
}

const cards = [
  "A♥",
  "A♠",
  "A♦",
  "A♣",
  "2♣",
  "3♣",
  "4♣",
  "5♣",
  "6♣",
  "7♣",
  "8♣",
  "9♣",
  "10♣",
  "K♣",
  "Q♣",
  "J♣",
  "2♦",
  "3♦",
  "4♦",
  "5♦",
  "6♦",
  "7♦",
  "8♦",
  "9♦",
  "10♦",
  "K♦",
  "Q♦",
  "J♦",
  "2♥",
  "3♥",
  "4♥",
  "5♥",
  "6♥",
  "7♥",
  "8♥",
  "9♥",
  "10♥",
  "K♥",
  "Q♥",
  "J♥",
  "2♠",
  "3♠",
  "4♠",
  "5♠",
  "6♠",
  "7♠",
  "8♠",
  "9♠",
  "10♠",
  "K♠",
  "Q♠",
  "J♠"
];
var hand = [];

function in_array(array, el) {
  for (var i = 0; i < array.length; i++) if (array[i] == el) return true;
  return false;
}

function get_rand(array) {
  var rand = array[Math.floor(Math.random() * array.length)];
  if (!in_array(hand, rand)) {
    hand.push(rand);
    return rand;
  }
  return get_rand(array);
}

for (var i = 0; i < 5; i++) {
  document.write(get_rand(cards));
}

ReactDOM.render(<Reset />, document.getElementById("root"));

Basically, what would I have to fill in the parts with "..." in order for the code to rerandomize the pack. 基本上,为了使代码重新随机化包,我必须在部分中添加“ ...”。

Try something like this, I'm preserving alot of the code you already wrote. 尝试类似这样的事情,我保留了您已经编写的代码。 You really just have to move that logic into the handler. 您实际上只需要将该逻辑移到处理程序中即可。

Here's the sandbox as well: https://codesandbox.io/s/yv93w19pkz 这也是沙箱: https : //codesandbox.io/s/yv93w19pkz

import React from "react";
import ReactDOM from "react-dom";

class App extends React.Component {
  constructor(props) {
      super(props);
      this.state = { 
        cards: ["A♥", "A♠", "A♦", "A♣", "2♣", "3♣", "4♣", "5♣", "6♣", "7♣", "8♣", "9♣", "10♣", "K♣", "Q♣", "J♣", "2♦", "3♦", "4♦", "5♦", "6♦", "7♦", "8♦", "9♦", "10♦", "K♦", "Q♦", "J♦", "2♥", "3♥", "4♥", "5♥", "6♥", "7♥", "8♥", "9♥", "10♥", "K♥", "Q♥", "J♥", "2♠", "3♠", "4♠", "5♠", "6♠", "7♠", "8♠", "9♠", "10♠", "K♠", "Q♠", "J♠"],
        hand: [] 
      }

       this.handleClick = this.handleClick.bind(this)
  }

  handleClick() {
    const cards = this.state.cards

    const newHand = []

    function get_rand(array) {
       var rand = array[Math.floor(Math.random() * array.length)];
       if (!newHand.includes(rand)) {
          newHand.push(rand);
       } else {
         get_rand(cards);
       }
     }

    for (var i = 0; i < 5; i++) {
       get_rand(cards);
    }

    this.setState({
      hand: newHand
    })

  }

  render() {
    const { hand } = this.state
      return (
        <div>
          { hand ? (hand.map((card) => {
            return <p>{card}</p>
           })) : (
            null
          )}
          <button onClick={this.handleClick}>Randomize
          </button>
        </div>
      );
  }
}

ReactDOM.render(
  <App />,
  document.getElementById('root')
);

Try to declare the cards in the state and update when you click on it 尝试声明卡的状态并在单击时更新

尝试声明卡的状态并在单击时更新

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

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