0

React

As you can see below I have 5 words and 4 colors. In my CardList I take the first 4 words (given at random). Then I want to pass in a color at random to each word. I have a shuffled_colors array that takes care of the randomness. Now i just want the ith color on the ith card. When I pass in props.colors it just passes in the entire array. I'm not sure how to get the ith color off of it

App.js

const App = () => {
  var shuffleSeed = require('shuffle-seed');
  const words = ['One','Two', 'Three','Four','Five']
  const color= ['blue','blue','red','red']
  var shuffled_words = shuffleSeed.shuffle(words,"Kappa2");
  var shuffled_color = shuffleSeed.shuffle(color,"teams");

  console.log(shuffled_words)
  return (
    <div className="App">
      <h1>CodeNames</h1> 
      <CardList words={shuffled_words} colors={shuffled_color }/>
    </div>
  );
}

CardList.component

export const CardList = (props) => (
  <div className='card-list'>
    {props.words.filter((word, idx) => idx < 4)
      .map(word => (
        <Card key={word} word={word} color={props.colors}/>
      ))}
  </div>
)

First every React question here. Let me know if I need anything else.

1 Answer 1

1

You need to use props.colors[index]

Working demo

Code snippet

export const CardList = props => (
  <div className="card-list">
    {props.words
      .filter((word, idx) => idx < 4)
      .map((word, index) => (
        <div key={word} word={word} color={props.colors[index]}>
          {word} '--' {props.colors[index]}
        </div>
      ))}
  </div>
);

export default function App() {
  var shuffleSeed = require("shuffle-seed");
  const words = ["One", "Two", "Three", "Four", "Five"];
  const color = ["blue", "blue", "red", "red"];
  var shuffled_words = shuffleSeed.shuffle(words, "Kappa2");
  var shuffled_color = shuffleSeed.shuffle(color, "teams");

  console.log(shuffled_words);
  return (
    <div className="App">
      <h1>CodeNames</h1>
      <CardList words={shuffled_words} colors={shuffled_color} />
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

Comments

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.