AuthContext.js 823 Bytes
import React, {createContext, useContext, useEffect, useState} from 'react';
import {useDispatch, useSelector} from 'react-redux';
import {LOAD_ME_SUCCESS} from "./reducers/user";

export const AuthContext = createContext({});
export const AuthProvider = (props) => {
    const {children, user} = props;

    const dispatch = useDispatch();

    const onLoadMe = async () => {
        try {
            await dispatch({
                type: LOAD_ME_SUCCESS,
                data: {
                    user
                }
            });
        } catch (e) {
            console.log(e);
        }
    };

    useEffect(() => {
        onLoadMe();
        console.log('AuthContext user', user);
    }, [user]);

    return (
        <AuthContext.Provider>
            {children}
        </AuthContext.Provider>
    )
};