😊

JWT Authentication【16Context API】

2022/10/18に公開

JWT Authentication【16Context API】

YouTube: https://youtu.be/2NqYwDrlx84

src/context/authContext.tsx
import { createContext, ReactNode, useContext, FC, useState } from "react";

interface Props {
  children: ReactNode
}

const AuthContext = createContext({})

export const AuthProvider: FC<Props> = ({children}) => {
  const [user, setUser] = useState(null)

  return (
    <AuthContext.Provider value={{user, setUser}}>
      {children}
    </AuthContext.Provider>
  )
}

export const useAuthContext = () => useContext(AuthContext)
src/index.tsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { AuthProvider } from './context/authContext';

const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement
);
root.render(
  <React.StrictMode>
    <AuthProvider>
      <App />
    </AuthProvider>
  </React.StrictMode>
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();

Discussion