React.js Map Array style for a unique id and each button disabled

Viewed 497

d

I need each question button when it is clicked to be disabled. I would also like to style each question button. I already looked for several answers to this problem but I found nothing. As I don't know much about React in this case, should I use useEffect or children.props? If you have any solution please let me know.

Demo

1 Answers

Your buttons' state is already available for each button, you just need to use state and disable button.

{/* Question buttons */}
{buttons.map((button) => (
  <button key={button.id} disabled={button.clicked} onClick={() => buttonClickHandler(button.id)}>
    {button.label}
  </button>
))}

About styling buttons individually, you can create a className for each button and style buttons depending on that:

<button className={'button-' + button.id}>

and now from css apply color:

.button-01 { background: pink; }

The second approach could be that you create styles for each button inside the state, and consume it from there.

<button style={button.style}>

Related