Skip to content

Instantly share code, notes, and snippets.

@flucivja
flucivja / react_hooks_testing_act_warning.js
Last active Feb 8, 2019
React hooks testing act warning
View react_hooks_testing_act_warning.js
// this is simplified example of testing react hook where I am getting warning that test code should be wrapped in act.
function usePromise(promise) {
const [data, setData] = useState({
isLoading: true,
data: null
});
useEffect(() => {
promise.then((res) => {
@flucivja
flucivja / react_hooks_state_reference_misunderstanding.js
Last active Jan 17, 2019
React useState - usage of state reference misunderstanding
View react_hooks_state_reference_misunderstanding.js
// This is the example of React component using useState React Hook which looks alright at first glance but it is not.
// Someone would think that "setText" would set text variable to new text and it can be used immediatelly, but this is not true.
// The setText does not update "text" variable immediatelly, it will update text state somewhere at React internals
// and after the component rerenders the "text" variable will be updated with new value.
// This is not some React magic, this is just how setter works.
// You must remember that functional component is basically the render method of class component
// and if state is changed in render method then component must rerender to access new state.
function Text() {
const [text, setText] = useState('');
You can’t perform that action at this time.