Skip to content

Instantly share code, notes, and snippets.

@chrisfosterelli
Last active September 1, 2022 13:28
Show Gist options
  • Save chrisfosterelli/2e523b4beae43f05624920b26bb55fa0 to your computer and use it in GitHub Desktop.
Save chrisfosterelli/2e523b4beae43f05624920b26bb55fa0 to your computer and use it in GitHub Desktop.

Revisions

  1. chrisfosterelli revised this gist Dec 17, 2021. 2 changed files with 10 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion AccountProvider.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ function AccountProvider({ children }) {
    const [account, setAccount] = useState(null)

    return (
    <AccountContext.Provider value={{ account, setAccount }}>
    <AccountContext.Provider value={[ account, setAccount ]}>
    {children}
    </AccountContext.Provider>
    )
    9 changes: 9 additions & 0 deletions MyComponent.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    import React from 'react'
    import useAccount from '../hooks/useAccount'

    function MyComponent() {
    const [account, setAccount] = useAccount()
    return <div>{account ? account.firstName : 'No Account'}</div>
    }

    export default MyComponent
  2. chrisfosterelli created this gist Dec 17, 2021.
    2 changes: 2 additions & 0 deletions AccountContext.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    import { createContext } from 'react'
    export default createContext(null)
    18 changes: 18 additions & 0 deletions AccountProvider.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    import React, { useState } from 'react'
    import AccountContext from '../contexts/AccountContext'

    function AccountProvider({ children }) {
    const [account, setAccount] = useState(null)

    return (
    <AccountContext.Provider value={{ account, setAccount }}>
    {children}
    </AccountContext.Provider>
    )
    }

    AccountProvider.propTypes = {
    children: PropTypes.node.isRequired
    }

    export default AccountProvider
    12 changes: 12 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    import React from 'react'
    import ReactDOM from 'react-dom'

    import App from './components/app/App'
    import AccountProvider from './providers/AccountProvider'

    ReactDOM.render(
    <AccountProvider>
    <App />
    </AccountProvider>,
    document.getElementById('root')
    )
    11 changes: 11 additions & 0 deletions useAccount.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    import { useContext } from 'react'
    import AccountContext from '../contexts/AccountContext'

    function useAccount() {
    const value = useContext(AccountContext)
    if (value === null)
    throw new Error('<AccountProvider> is not in the parent tree')
    return value
    }

    export default useAccount