简体   繁体   English

使用 React.js 的随机数

[英]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/ JSFIDLLE: https://jsfiddle.net/1cban4oy/12/

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/ JSFIDLLE: https ://jsfiddle.net/1cban4oy/12/

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/ JSFIDLLE: https ://jsfiddle.net/1cban4oy/12/

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/ JSFIDLLE: https ://jsfiddle.net/1cban4oy/12/

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/ JSFIDLLE: https ://jsfiddle.net/1cban4oy/12/

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/ JSFIDLLE: https ://jsfiddle.net/1cban4oy/12/

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/ JSFIDLLE: https ://jsfiddle.net/1cban4oy/12/

import React, { useState } from "react";

const App = () => {
  const [num, setNum] = useState(0);

  function randomNumberInRange(min, max) {
    //  get number between min and max
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

  const handleClick = () => {
    setNum(randomNumberInRange(1, 5));
  };

  return (
    <div>
      <h2>number is: {num}</h2>
      <button onClick={handleClick}>Generate random number</button>
    </div>
  );
};

export default App;

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

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