1

My goal here is to have a box that renders the word of a color in order within the array.

I'm struggling with the concept on rendering each element of the array. I'm assuming you'd need to pick out each element, store it in a variable, and render the variable but I'm hitting a dead end each time I attempt it.

You'll see my commented out attempt in the code below. I also tried forEach but React gave me an error while trying to use forEach.

Additionally, I was told to avoid using .map and a for loop if possible.

import React from 'react'

class ColorSwitch extends React.Component {
    constructor(props) {
        super(props)
            this.state = {
                colors: ["white", "orange", "purple", "black", "green"]
            }
    }

    nextColor = () => {
        let newColor = this.state.colors

        // let setColor = newColor[0] += 1 (DIDNT WORK)

        this.setState({colors: setColor})
    }

    render() {
        let { colors } = this.state
        return(
            <div className="box" onClick={this.nextColor}>
            <p>{this.setColor}</p>
            </div>
        )
    }
}

export default ColorSwitch

Thanks in advance for your help.

1
  • Have you tried let setColor = newColor[0] + 1? Commented Feb 26, 2019 at 23:05

2 Answers 2

1

Get the colors from the props (or use a const if it's static), and store the currentColorIndex inside the state.

When calling nextColor increment the currentColorIndex by 1 (I've used % colors.length to make next cyclic). To render grab the color at the currentColorIndex:

const colors = ["white", "orange", "purple", "black", "green"]

class ColorSwitch extends React.Component {
    state = {
      currentColorIndex: 0
    }

    nextColor = () =>
      this.setState(({ currentColorIndex }) => ({
        currentColorIndex: (currentColorIndex + 1) % this.props.colors.length
      }))

    render() {
        const { currentColorIndex } = this.state
        const { colors } = this.props
        
        const color = colors[currentColorIndex];
        
        return(
            <div className="box" 
            onClick={this.nextColor} 
            style={{ background: color }}>
              <p>{color}</p>
            </div>
        )
    }
}

ReactDOM.render(
  <ColorSwitch colors={colors} />,
  demo
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="demo"></div>

Sign up to request clarification or add additional context in comments.

Comments

0

I don't know who told you to stay away from .map because that's how you should do it.

import React from 'react'

class ColorSwitch extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
           colors: ["white", "orange", "purple", "black", "green"]
        }
    }

    render() {
        let { colors } = this.state
        return(
            <div>
              {colors.map(color => 
                 <div className="box">
                   <p>{color}</p>
                 </div>
              )}
            </div>
        )
    }
}

export default ColorSwitch

1 Comment

You kind of got baited into this answer because op mentioned forloops and .map, but it looks like they actually only want to render one color at a time, so they don't want to do a mapping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.