I want to achieve something like this, when I click on 20 numbered block, everything before that changes to light orange.
I have this attached sandbox code, if anyone can guide what to add in the handleClick
function would be grateful
https://codesandbox.io/s/adoring-cherry-by6u9b?file=/demo.tsx
5
Answers
You can manage one state to store the selected block. Now, we can pass props to block based on this state.
Check out the working demo here:
https://codesandbox.io/s/floral-leaf-neuuku?file=/demo.tsx
Made some changes in your playground: codesandbox.io
Basic idea is that you need to save the current clicked index and then to pass the custom backgroundColor property to your styled component if the element’s index is smaller than the clicked index.
You can also do it like that although i am not sure if it’s very efficient.
change demo.tsx:
Here is the two key points:
and,
This answer use the same theme as you have used and does not break any style.
Store the index of the item clicked. And based on it, update the background color.
https://codesandbox.io/s/happy-leaf-348miz?file=/demo.tsx