This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const UserContext = React.createContext(); | |
| const App = () => { | |
| const username = "John Doe"; | |
| const otherData = {}; | |
| return ( | |
| <UserContext.Provider value={{ username, otherData }}> | |
| <Header /> | |
| </UserContext.Provider> | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // With empty array | |
| useEffect(() => { | |
| console.log('I will run only once'); | |
| }, []); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Without the second parameter | |
| useEffect(() => { | |
| console.log('I will run after every render'); | |
| }); | |
| // With the second parameter | |
| useEffect(() => { | |
| console.log('I will run only when valueA changes'); | |
| }, [valueA]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| import useGlobalHook from 'use-global-hook'; | |
| const initialState = { | |
| counter: 0, | |
| }; | |
| const actions = { | |
| addToCounter: (store, amount) => { | |
| const newCounterValue = store.state.counter + amount; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function setState(newState) { | |
| this.state = { ...this.state, ...newState }; | |
| this.listeners.forEach((listener) => { | |
| listener(this.state); | |
| }); | |
| } | |
| function useCustom(React) { | |
| const newListener = React.useState()[1]; | |
| React.useEffect(() => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| import useGlobalHook from './useGlobalHook'; | |
| const initialState = { counter: 0 }; | |
| const useGlobal = useGlobalHook(React, initialState); | |
| export default useGlobal; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function setState(newState) { | |
| this.state = { ...this.state, ...newState }; | |
| this.listeners.forEach((listener) => { | |
| listener(this.state); | |
| }); | |
| } | |
| function useCustom(React) { | |
| const newListener = React.useState()[1]; | |
| React.useEffect(() => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const useCustom = () => { | |
| const newListener = useState()[1]; | |
| useEffect(() => { | |
| // Called just after component mount | |
| listeners.push(newListener); | |
| return () => { | |
| // Called just before the component unmount | |
| listeners = listeners.filter(listener => listener !== newListener); | |
| }; | |
| }, []); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| import useCustom from './customHook'; | |
| const Counter = () => { | |
| const [globalState, setGlobalState] = useCustom(); | |
| const add1Global = () => { | |
| const newCounterValue = globalState.counter + 1; | |
| setGlobalState({ counter: newCounterValue }); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useState, useEffect } from 'react'; | |
| let listeners = []; | |
| let state = { counter: 0 }; | |
| const setState = (newState) => { | |
| state = { ...state, ...newState }; | |
| listeners.forEach((listener) => { | |
| listener(state); | |
| }); |