Created
July 30, 2023 07:17
-
-
Save dballowe7912/831c4dc24f07fde6c3a3c6c7cb282652 to your computer and use it in GitHub Desktop.
React Context Setup
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, { createContext, useContext, useReducer } from "react"; | |
// Prepares the dataLayer | |
export const StateContext = createContext(); | |
// Wrap our app and provide the Data layer | |
export const StateProvider = ({ reducer, initialState, children }) => ( | |
<StateContext.Provider value={useReducer(reducer, initialState)}> | |
{children} | |
</StateContext.Provider> | |
); | |
// Pull information from the data layer | |
export const useStateValue = () => useContext(StateContext); |
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 ReactDOM from "react-dom"; | |
import "./index.css"; | |
import App from "./App"; | |
import * as serviceWorker from "./serviceWorker"; | |
import reducer, { initialState } from "./reducer"; | |
import { DataLayer } from "./DataLayer"; | |
ReactDOM.render( | |
<React.StrictMode> | |
<DataLayer initialState={initialState} reducer={reducer}> | |
<App /> | |
</DataLayer> | |
</React.StrictMode>, | |
document.getElementById("root") | |
); | |
// If you want your app to work offline and load faster, you can change | |
// unregister() to register() below. Note this comes with some pitfalls. | |
// Learn more about service workers: https://bit.ly/CRA-PWA | |
serviceWorker.unregister(); |
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
export const initialState = {}; | |
const reducer = (state, action) => { | |
console.log(action); | |
switch(action.type) { | |
default: | |
return state; | |
} | |
}; | |
export default reducer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment