Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Creating an input for a dice roller in React (javascript)

I am making a dice roller in javascript with React. Basically, I have an array with dice values and a loop that runs through them to render dice for the user to roll. I'm now trying to create an input so the user can make a new die with any number of sides, but I'm having trouble connecting that input to the rest of my code. Please let me know if you have any ideas about how I could do this with javascript! Thanks!

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';

function RenderButtons(props){
  return(
      <button className="center" data-index={props.index} id="roller" onClick={props.onClick}>
        {props.value.current} /{props.value.max}
        </button>
  )
}

class Roller extends React.Component {
  constructor(props) {
    super(props);
    this.roll = this.roll.bind(this)
    this.state = {
      diceValues: [
        {max:100, current:100},
      ],
    }
  }

  roll(evt) {
    let min = 1;
    let max = this.state.diceValues[evt.target.dataset.index].max
    let result = Math.floor(Math.random() * (max - min) + min);

    const diceValues = this.state.diceValues.slice();
    diceValues[evt.target.dataset.index].current = result + 1;
    this.setState({diceValues: diceValues});
  }

  makeDie(i) {
    return(
      <RenderButtons
      key = {i}
      onClick = {this.roll}
      value = {this.state.diceValues[i]}
      index = {i}
      />
    )
  }
  render() {
    let buttonsToMake = [];
    for (let i = 0; i < 1; i++){
      console.log("rendering buttons")
      buttonsToMake.push(this.makeDie(i))
      return (
        <div>
          <input
          type="number"
          placeholder="Custom Dice"
          ></input>
          <button
          // id="input"
          // value={this.state.value}
          // onChange={console.log(this.state.value)}
          // {this.handleChange}
          >
          Generate Die
          </button>
          {buttonsToMake}
        </div>
        )
    }
  }
}

ReactDOM.render(
  <Roller />,
  document.getElementById('root')
);
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!