Skip to content

Instantly share code, notes, and snippets.

@InputNeuron
Created April 27, 2020 13:58
Show Gist options
  • Select an option

  • Save InputNeuron/1f21123a36134bc3e0f5c38d46f01e8a to your computer and use it in GitHub Desktop.

Select an option

Save InputNeuron/1f21123a36134bc3e0f5c38d46f01e8a to your computer and use it in GitHub Desktop.
import React, {useCallback, useState} from "react";
import Button from "@material-ui/core/Button";
interface IProps {
readonly value: number;
readonly setValue: (value: number) => void;
}
const ChildComponent = (props: IProps) => {
const value = props.value;
console.log("ChildComponent: render");
return (
<>
value is: {value}
<Button variant="contained"
color="primary"
onClick={() => props.setValue(value + 1)}>
Update
</Button>
</>
)
}
export const SharedStateTest = () => {
const [state, setState] = useState(1);
// const [state, setState] = useSharedState(1);
// const getValue = useCallback(() => state, []);
const setValue = useCallback((value) => setState(value), []);
console.log("SharedStateTest: render");
return (
<ChildComponent value={state} setValue={setValue}/>
)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment