I'm working on an app where I want to change a value from lbs to kgs. I currently have the state set to lbs and am trying to add a toggle switch which will allow users to toggle between lbs and kgs. I'm having trouble passing the state between sibling components.
Here is my code:
const HomePage = (props) => {
const {
movements
} = props;
const convertToKg = (_id) => {
const kg = movements.map((movement) => {
return {_id: movement._id, movementName: movement.movementName, movementWeight: movement.movementWeight * 0.453592}
})
return kg;
}
return (
<div>
<div>
<MovementButtons movements={movements} />
</div>
<div>
<WeightConverter onClick={convertToKg} />
</div>
</div>
);
};
export default HomePage;
and my toggle switch code:
const WeightConverter = (props) => {
const {
onClick
} = props;
return (
<div>
lb
<Switch
onClick={onClick}
>
</Switch>
kg
</div>
)
};
I'm not even sure I'm using the toggle switch correctly.
Basically what I want is for the 'movements' state to change to the result of convertToKg when the toggle switch is activated, and then back to it's original state when it is toggled back. If I need to show more code or answer any questions please ask. I just need a nudge in the right direction.
movementsis a React prop in the code example you provided. You will need to updatemovementswherever it is declared. Can you edit the post to include a complete minimal reproducible example of all the relevant code you are working with?