I'm trying to build a react component that allows a die to be rolled and update the total of the die roll on the page.I'm only including what I perceive to be necessary as the function is pretty long but this should cover the problems I'm having
import "./Dice.css";
import img from "./img/D8.png";
class Dice extends Component {
constructor(props) {
super(props);
this.state = {
bonusRoll: 0,
penaltyRoll: 0,
totalRoll: 1
};
this.rollDie = this.rollDie.bind(this);
};
rollDie(bonus,penalty) {
let rolls = []
const reducer = (previousValue, currentValue) => previousValue + currentValue;
console.log("test1")
if (!bonus && !penalty){
console.log("test2")
for (let i = 0; i < 2; i++) {
rolls.push(Math.ceil(Math.random() * 8)) * i; // within my code this is line 41
};
this.setState({totalRoll: rolls.reduce(reducer)});
// return rolls.reduce(reducer) // this worked when the function was called in vanilla JS
}};
render() {
return (
<div className="Dice">
<div className="Dice-rolls-wrap">
<div className="Dice-base-roll">
<h5>Baseroll: {this.state.totalRoll}</h5>
</div>
</div>
<button className="Dice-btn" onClick={this.rollDie}>
<img className="Dice-img" src={img} alt="Dice icon" />
</button>
</div>
);
}
}
export default Dice;
with the above code I get the following error when trying to run/build the app
src/Dice.js
Line 41:9: Expected an assignment or function call and instead saw an expression no-unused-expressions
Search for the keywords to learn more about each error.
if I add a return statement on line 41 everything renders but when clicking the button it will not proceed past the conditional (EG: "test1" shows in console but "test2" doesn't) I'm not passing in arguments to the function so it should be responding to this first bit of logic as far as I can tell but I'm not sure where I'm going wrong. (I'm also probably using setState wrong but I can't get the code to reach there yet to say). Also please be gentle this is my first Stack Overflow question