Skip to content

Instantly share code, notes, and snippets.

@celsofabri
Created September 16, 2020 13:19
Show Gist options
  • Save celsofabri/c84a3b72d294e93e82031998ef885201 to your computer and use it in GitHub Desktop.
Save celsofabri/c84a3b72d294e93e82031998ef885201 to your computer and use it in GitHub Desktop.
Creating a context layer in React App
import React, { createContext, useState } from 'react';
import initialValues from './initial';
const Context = createContext(initialValues);
const GlobalContext = ({ children }) => {
const [globalState, setGlobalState] = useState(initialValues);
return (
<Context.Provider value={{ globalState, setGlobalState }}>
{children}
</Context.Provider>
);
};
export { GlobalContext, Context };
@celsofabri
Copy link
Author

InitialValues

const initialValues = {
  homeScreen: true,
  userName: '',
  reposList: [],
  page: 1,
  currentRepo: {},
  isEditing: false,
  searchTerm: '',
  loading: false
};

export default initialValues;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment