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.
You are kinda in right direction but this are the mistakes you are making:
I want you to explore this:
I hope this helps, I am not giving you a code directly since you are asking to be pushed in right direct but do reply if you need a little extra help.