I have a state in parent component which i want to update it from onClick
in the child component, but it is not updating what is wrong here?
Another Problem I want to show a popup when the user click in one of the radio button. And i have different popup messages for each radio button. I am using the index to show this popup. But how can i tell the child component to show this clicked popup?
Parent
const [showPopup, setShowPopup] = useState(false);
const [activeIndex, setActiveIndex] = useState(-1);
return (
<div>
{options?.map((option,index) => (
<RadioBtn
key={index}
onChange={radioButtonChangeHandler}
label={option.label}
value={option.value}
showPopup={showPopup}
activeIndex={activeIndex}
onClick={() => {
setShowPopup(curr => !curr);
setActiveIndex(index);
}}
/>
))}
</div>
);
Child
const popupMessage = (<div>{infoContent</div>);
return (
<div className={clsx("radio-button", infoContent && "has-info")}>
<Popup open={showPopup}>
<PopupPositionReference>
<div className="radio-button-popup-icon onClick={props.onClick}>
<input
ref={iRef => {
ref(iRef);
inputRef.current = iRef;
}}
type="radio"
checked={isChecked}
{...props}
{...rest}
/>
<label htmlFor={id}>
{label}
</label>
</div>
</PopupPositionReference>
{popupMessage && <PopupContent>{popupMessage}</PopupContent>}
</Popup>
</div>
);
CSS
.radio-button {
&.has-info {
.radio-button-popup-icon {
position: relative;
&::after {
content: " ";
cursor: pointer;
display: block;
position: absolute;
top: 0;
right: 0;
width: 25px;
height: 25px;
background-image: url(...);
background-position: right;
background-size: contain;
}
}
}
``