there is a component duplicated many times in another component. There is a button in this component. How do I change the style of the first component when clicking on it
<div>
<Checkbox />
<Checkbox />
<Checkbox />
<Checkbox />
<Checkbox />
<Checkbox />
</div>
2
Answers
You can pass the optional props "style" from the parent component.
for example:
and that’s how you can handle this in your Checkbox component –
You’ll require state somewhere but depending on what component controls it will depend on where it goes. Ashish has hinted at one solution (controlling the state from the parent) but probably the easiest method is to have the
<Checkbox />
component control it instead.In this example I have an optional
styleCanChange
prop (defaults tofalse
) that tells the component whether or not to use theblue
style or not. I’ve added labels for completeness.