I looked through all the posts with similar title but found nothing that helped me understand my issue and solve it.
I have created a context that passes down the state of a switch (toggled or not). Problem is consumers (children) are not receiving changed context value (which is set through a state). It's a primary value, a boolean, not an object or array so reconstruction is not necessary. I have no idea what I could be doing wrong.
const Price = ({ showYearlyPrice }) => {
function getPriceParts(showYearlyPrice: boolean){
return showYearlyPrice ? "a" : "b";
}
const mainPrice = getPriceParts(showYearlyPrice);
return (
<>
<div className="flex flex-row">
<p className="text-grey text-sm">
{mainPrice}
</p>
</>
);
};
const PricingHeader = ({
price,
}) => {
// Subscribe to switch event
const isToggled = useContext(SwitchContext);
console.log(isToggled)// Only prints once, with default value every time
return (
<SubscribableSwitch color={sectionBackground}>
<Section bg={sectionBackground} spacing={SectionSpacing.BOTTOM_ONLY}>
<Price showYearlyPrice={isToggled as boolean} price={price}/>
</Section>
</SubscribableSwitch>
);
};
export default PricingHeader;
Then the actual SubscribableSwitch component, where the toggle works great and receives updated context value.
export const SwitchContext = createContext(false); // Default value, if I put "potato" that's what gets printed in children
const Toggle = ({ toggle }) => {
const isToggled = useContext(SwitchContext);
return (
<div className="flex justify-center mb-8 mt-2">
<h2 onClick={toggle}>click me</h2>
{!isToggled && (
<span>
Not toggled
</span>
)}
</div>
);
};
const SubscribableSwitch = ({color, children}) => {
const [isToggled, setToggle] = useState(false);
const toggle = () => setToggle((value) => !value);
return (
<SwitchContext.Provider value={isToggled}>
<Toggle toggle={toggle}/>
{children} // Where children is PricingHeader
</SwitchContext.Provider>
);
};
export default SubscribableSwitch;