Question

In: Computer Science

Use your favorite text editor or IDE to search for occurrences of setState. Where you found...

  1. Use your favorite text editor or IDE to search for occurrences of setState.

  2. Where you found uses of setState, refactor the code to use JavaScript functions instead of classes and the useState hook instead of setState.

    import React from 'react'
    import ColorSlider from './ColorSlider'
    import ColorOutput from './ColorOutput'
    import styles from './ColorBrowser.module.css'

    class ColorBrowser extends React.Component {
    constructor(props) {
    super(props)
    this.state = {
    red: Math.floor(Math.random() * 256),
    green: Math.floor(Math.random() * 256),
    blue: Math.floor(Math.random() * 256)
    }
    }
    updateColor(e) {
    this.setState({
    [e.target.name]: Number(e.target.value)
    })
    }
    getRandomColor() {
    this.setState({
    red: Math.floor(Math.random() * 256),
    green: Math.floor(Math.random() * 256),
    blue: Math.floor(Math.random() * 256)
    })
    }
    render() {
    return (
    <section className={styles["color-browser"]}>
    <h1>Welcome to the Color Browser</h1>
    <div className={styles.sliders}>
    <ColorSlider
    name="red"
    label="Red"
    min={0}
    max={255}
    value={this.state.red}
    onChange={this.updateColor.bind(this)}
    />
    <ColorSlider
    name="green"
    label="Green"
    min={0}
    max={255}
    value={this.state.green}
    onChange={this.updateColor.bind(this)}
    />
    <ColorSlider
    name="blue"
    label="Blue"
    min={0}
    max={255}
    value={this.state.blue}
    onChange={this.updateColor.bind(this)}
    />
    </div>

    <ColorOutput
    red={this.state.red}
    green={this.state.green}
    blue={this.state.blue}
    />
    <br/>
    <button onClick={this.getRandomColor.bind(this)}>Random Color</button>
    </section>
    )
    }
    }

    export default ColorBrowser

Solutions

Expert Solution

Implementation in Hooks:

import React, {useState} from 'react'

import ColorSlider from './ColorSlider'

import ColorOutput from './ColorOutput'

import styles from './ColorBrowser.module.css'

const colorBrowser = (props) => {

    //colorBrowserState keeps track of current state

    //setState is a function returned to set a new state

    const [colorBrowserState, setState] = useState({

        red: Math.floor(Math.random() * 256),

        green: Math.floor(Math.random() * 256),

        blue: Math.floor(Math.random() * 256)

    });

const updateColor = (e) => {

        //setState in Hooks doesn't merge the state but replaces it. Therefore we spread the currentState too

        setState({

            ...colorBrowserState,

            [e.target.name]: Number(e.target.value)

        });

    }

const getRandomColor = () => {

        //setState in Hooks doesn't merge the state but replaces it. Therefore we spread the currentState too

        setState({

            ...colorBrowserState,

            red: Math.floor(Math.random() * 256),

            green: Math.floor(Math.random() * 256),

            blue: Math.floor(Math.random() * 256)

        })

    }

    return (

        <section className={styles["color-browser"]}>

            <h1>Welcome to the Color Browser</h1>

            <div className={styles.sliders}>

                <ColorSlider

                    name="red"

                    label="Red"

                    min={0}

                    max={255}

                    value={colorBrowserState.red}

                    onChange={updateColor}

                />

                <ColorSlider

                    name="green"

                    label="Green"

                    min={0}

                    max={255}

                    value={colorBrowserState.green}

                    onChange={updateColor}

                />

                <ColorSlider

                    name="blue"

                    label="Blue"

                    min={0}

                    max={255}

                    value={colorBrowserState.blue}

                    onChange={updateColor}

                />

            </div>

            <ColorOutput

                red={colorBrowserState.red}

                green={colorBrowserState.green}

                blue={colorBrowserState.blue}

            />

            <br />

            <button onClick={getRandomColor}>Random Color</button>

        </section>

    )

}

export default colorBrowser;

Please comment if you need any clarification or if you want some tweaks


Related Solutions

The vi text editor can be a bit challenging to use at first, but once you...
The vi text editor can be a bit challenging to use at first, but once you have familiarized yourself with how it works you will find it an efficient way to create and edit text files in Linux, and UNIX as well. The key thing to remember is that vi has two operational modes: insert mode and command mode. In insert mode, everything you type is entered into your file. You can use the backspace, delete and the arrow keys....
Use your favorite search engine and search for “world’s greatest data breaches and hacks.” Scan through...
Use your favorite search engine and search for “world’s greatest data breaches and hacks.” Scan through the hits until you find visual diagrams or a text-based list of major data breaches that have occurred recently. (Major data breaches are defined as those in excess of 30,000 records.) Select and carefully review at least two of these data breaches. Briefly describe the two data breaches you selected. Explain in layman’s terms how you think these breaches occurred. Discuss whether or not...
Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt)...
Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE: (write your program so that...
you can use either vim or nano as text editor Implement the following code in ARM...
you can use either vim or nano as text editor Implement the following code in ARM on Raspberry Pi, compile and run. g=12, h=8, i=2, j=5; f = (g + h) - (i + j); Your program displays the message: f = (g + h) – (i + j) = 13 Note: answer should be calculated not hardcoded
  Why do you think Google dominates as a search engine?  What's your favorite search engine and why...
  Why do you think Google dominates as a search engine?  What's your favorite search engine and why -- or do you not prefer one over the other?  What do you think makes a good search engine?  Can you think of any features that search engines could potentially add? answer in 1-2 paragraphs
Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice)....
Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice). This program behaves similarly to the AddValueToArray program from before. However, instead of modifying the array in-place, it will return a new array holding the modification. The original array does not change. For simplicity, the array used is “hard-coded” in main, though the method you write should work with any array. Example output with the command-line argument 3 is shown below: Original array: 3...
Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice)....
Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice). This program contains two methods which you will need to write: swapRows: Swaps the contents of two rows, given a two-dimensional array and the indices of the rows to swap. swapCols: Swaps the contents of two columns, given a two-dimensional array and the indices of the columns to swap. main contains some code which will call swapRows and swapCols, for the purpose of informal...
Download the Take.java file, and open it in jGrasp (or a text editor of your choice)....
Download the Take.java file, and open it in jGrasp (or a text editor of your choice). This program will “take” a given number of elements from a given array, using the command-line arguments as an array, and the hard-coded value of 3 elements to take. Example output of this program with the command-line arguments foo bar baz is shown below: foo bar baz In the above example, three elements were taken. However, it's possible that we may request to take...
Download the AllEqual.java file, and open it in jGrasp (or a text editor of your choice)....
Download the AllEqual.java file, and open it in jGrasp (or a text editor of your choice). This program takes a number of command line arguments, converts them to integers, and then determines if they all have the same value. An example run of the program is below, using the command-line arguments 1 2 3: Are equal: false A further example is shown below, with the command-line arguments 2 2: Are equal: true In the event that the program is given...
Download the Compass.java file, and open it in jGrasp (or a text editor of your choice)....
Download the Compass.java file, and open it in jGrasp (or a text editor of your choice). This program will randomly print out a compass direction, given a seed value for produce a random number with java.util.Random. Compass directions are mapped to integers according to the following table: Compass Direction Integer ID North 0 Northeast 1 East 2 Southeast 3 South 4 Southwest 5 West 6 Northwest 7 Further details are in the comments of Compass.java. import java.util.Random; import java.util.Scanner; public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT