#Tenzies app working on scrimba but not on vs code

9 messages · Page 1 of 1 (latest)

lapis anvil
#

The code works till i style the held dice to green but the moment i introduce the holdDice function it shows this error, i tried everything and error still seems to remain. It looks like the error is somewhere near the spread syntax, i even tried naming each of the properties individually, it still did not work

import React from "react"
import Die from "./Die"
import {nanoid} from "nanoid"

export default function App() {

    const [dice, setDice] = React.useState(allNewDice())
    
    function generateNewDie() {
        return {
            value: Math.ceil(Math.random() * 6),
            isHeld: false,
            id: nanoid()
        }
    }
    
    function allNewDice() {
        const newDice = []
        for (let i = 0; i < 10; i++) {
            newDice.push(generateNewDie())
        }
        return newDice
    }
    
    function rollDice() {
        setDice(oldDice => oldDice.map(die => {
            return die.isHeld ? 
                die :
                generateNewDie()
        }))
    }
    
    function holdDice(id) {
        setDice(oldDice => oldDice.map(die => {
            return die.id === id ? 
                {...die, isHeld: !die.isHeld} : //error on this line
              //^
                die
        }))
    }
    
    const diceElements = dice.map(die => (
        <Die 
            key={die.id} 
            value={die.value} 
            isHeld={die.isHeld} 
            holdDice={() => holdDice(die.id)}
        />
    ))
    
    return (
        <main>
            <div className="dice-container">
                {diceElements}
            </div>
            <button className="roll-dice" onClick={rollDice}>Roll</button>
        </main>
    )
}
empty nest
#

Your line 54

You closed the line } before your else statement

Your : is to be inside the {} as well

tall cove
#

Have you found solution to your question?

granite summit
#

@lapis anvil So this webpack version which bundles the files to generate the project on scrimba.com is a little different than what you usually would run when working on a react project locally.

Because of that this webpack configuration doesn't support the spread or rest operator out of the box and you will need to install a plugin for webpack to enable that. And then once that is installed you can enable the plugin in the webpack config file.

Step 1: cd in to your project and install
npm i babel-plugin-transform-object-rest-spread

Step2: After installation, open your webpack.config.js and below the presets add the plugin like this.

"presets": [
"babel-preset-env",
"babel-preset-react"
],
"plugins": ["babel-plugin-transform-object-rest-spread"]

After that you can use npm run build command to build the index.html.

Although this being so messy and just a band aid solution I would recommend to not use this to deploy, in future you will learn a better way to run and build your React projects.

solar bloom
#

the project is the Notes app

granite summit
solar bloom
#

It worked, Thanks @granite summit I was missing activating the live server