Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number using React.js

I wrote this code, but when I run it, I only get a blank page. What is wrong? It does seem that I am close to the answer. I've tried everything, but it is still not working.

class Button extends React.Component {

  constructor(props) {
   super(props);
   this.state = {
   random: 0
    }
   }


   render() {
   var min = 1;
   var max = 100;
   var rand =  min + (Math.random() * (max-min));
   handleClick() {
    this.setState ({this.state.random + this.rand})
   }
    return (
      <div>
       <button value="Click me!" onClick={this.handleClick.bind(this)></button>
       </div>
      );

 React.render(<Button />, document.querySelector('#container'));

  }
} 

JSFIDLLE: https://jsfiddle.net/1cban4oy/12/

like image 919
Damien Asseya Avatar asked Jul 18 '17 19:07

Damien Asseya


People also ask

How do you generate a random number in JavaScript?

Javascript creates pseudo-random numbers with the function Math. random() . This function takes no parameters and creates a random decimal number between 0 and 1. The returned value may be 0, but it will never be 1.

What does math random () do?

The Math. random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range.

How do you generate random numbers?

Computers can generate truly random numbers by observing some outside data, like mouse movements or fan noise, which is not predictable, and creating data from it. This is known as entropy. Other times, they generate “pseudorandom” numbers by using an algorithm so the results appear random, even though they aren't.

How do you generate a random number between 1 and 6 in Java?

For example, in a dice game possible values can be between 1 to 6 only. Below is the code showing how to generate a random number between 1 and 10 inclusive. Random random = new Random(); int rand = 0; while (true){ rand = random.

How to generate a random number in react?

Use the Math.random () function to generate a random number in React, e.g. Math.floor (Math.random () * (max - min + 1)) + min. The Math.random function returns a number in the range 0 to less than 1 but can also be used to generate a number in a specific range.

How to create random numbers in JavaScript?

JavaScript Random 1 Math.random (). Math.random () always returns a number lower than 1. 2 JavaScript Random Integers. Math.random () used with Math.floor () can be used to return random integers. There is no... 3 A Proper Random Function. As you can see from the examples above, it might be a good idea to create a proper random... More ...

How to return a random number between Min and Max in JavaScript?

This JavaScript function always returns a random number between min (included) and max (excluded): Example. function getRndInteger (min, max) {. return Math.floor(Math.random() * (max - min) ) + min; }.


2 Answers

Remove all your logic from the render function and add it to the click handler method. Also the onClick is missing a curly bracket at the end. Finally you're not indicating the state property you're updating in the setState() method.

This basically seems to do what you're after: https://codesandbox.io/s/98n9EkEOJ

This is the code just in case:

import React from 'react';
import { render } from 'react-dom';

class Button extends React.Component {

  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
    this.state = { random: 0 };
  }

  handleClick() {
    const min = 1;
    const max = 100;
    const rand = min + Math.random() * (max - min);
    this.setState({ random: this.state.random + rand });
  }

  render() {
    return (
      <div>
        <button onClick={this.handleClick.bind(this)}>Click</button>
        <div>The number is: {this.state.random}</div>
      </div>
    );
  }
}

render(<Button />, document.getElementById('container'));
like image 192
Rodrigo Avatar answered Oct 19 '22 07:10

Rodrigo


  • Firstly, you didn't set the state properly.
  • Secondly, use arrow functions so you can avoid problematic binding.
  • Thirdly, you didn't display the random value anywhere.
  • Lastly - you can move the min and max variables outside the render function. Also the whole math logic responsible for rolling a random number can be moved into the handleClick function.

Working code:

class Button extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      random: null,
    }
  }

  min = 1;
  max = 100;

  handleClick = () => {
    this.setState({random: this.min + (Math.random() * (this.max - this.min))});
  };

  render() {
    return (
      <div>
        <button onClick={this.handleClick}>Click me</button>
        {this.state.random}
      </div>
    );
  }
}
like image 9
kind user Avatar answered Oct 19 '22 05:10

kind user