From 9534e4248bc0d780eec6fa0747df56569bf6c8f7 Mon Sep 17 00:00:00 2001 From: Maks Yadvinskyy Date: Tue, 1 Aug 2023 00:11:23 -0500 Subject: [PATCH] docs(core): Update documentation and usage --- README.md | 49 ++++++++++++++++++++++-------- examples/context/Custom.context.js | 12 ++------ examples/index.js | 24 +++++++-------- 3 files changed, 51 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 9b56323..fb4d673 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ This package was created to help clean up the nasty code you get from taking advantage of React Context API. Transform your code from something that looks like this -```html + +```tsx @@ -16,7 +17,8 @@ Transform your code from something that looks like this ``` to something like this -```html + +```tsx , @@ -31,6 +33,7 @@ to something like this ## Installation To install the package, run the following command in your terminal + ```shell npm install react-multi-provider ``` @@ -38,21 +41,43 @@ To install the package, run the following command in your terminal ## Usage To use this component it is really simple and easy. Start by importing it after installation. + ```javascript - import MultiProvider from 'react-multi-provider'; +import { MultiProvider } from "react-multi-provider"; ``` Then take the nested providers and pass them into a `providers` prop. -```html - , - - ]}> - - + +```tsx +, + , + ]} +> + + +``` + +To consume the context, leverage the `useContext` hook. + +```tsx +export const ChildComponent = () => { + const fooValue = useContext(FooContext); + const barValue = useContext(BarContext); + + return ( + <> +

+ Hello, I am {fooValue} and I am {barValue} old +

+ + ); +}; ``` -Check out a more detailed example in the `examples/src/` directory of this repo. +Check out a more detailed example in the `examples/` directory of this repo. ## Notes -For those of you who are Flutter fans will know that the inspiration for this package came from `provider`. Special thanks to the author! \ No newline at end of file + +For those of you who are Flutter fans will know that the inspiration for this package came from `provider`. Special thanks to the author! diff --git a/examples/context/Custom.context.js b/examples/context/Custom.context.js index d74a733..079bfcb 100644 --- a/examples/context/Custom.context.js +++ b/examples/context/Custom.context.js @@ -1,10 +1,4 @@ -import React from 'react'; +import React from "react"; -const FooContext = React.createContext({}); -const BarContext = React.createContext({}); - -export const FooProvider = FooContext.Provider; -export const FooConsumer = FooContext.Consumer; - -export const BarProvider = BarContext.Provider; -export const BarConsumer = BarContext.Consumer; \ No newline at end of file +export const FooContext = React.createContext(""); +export const BarContext = React.createContext(0); diff --git a/examples/index.js b/examples/index.js index 649e830..921a4e2 100644 --- a/examples/index.js +++ b/examples/index.js @@ -1,18 +1,13 @@ -import React from "react"; +import React, { useContext } from "react"; import { render } from "react-dom"; -import { MultiProvider } from "../dist/react-multi-provider"; +import { MultiProvider } from "react-multi-provider"; // Example Context -import { - FooConsumer, - FooProvider, - BarConsumer, - BarProvider, -} from "./context/Custom.context"; +import { BarContext, FooContext } from "./context/Custom.context"; const App = () => { const providerList = [ - , - , + , + , ]; return ( @@ -27,11 +22,14 @@ const Parent = (props) => { }; const Child = () => { + const fooValue = useContext(FooContext); + const barValue = useContext(BarContext); + return ( <> -

Consumer

- {(fooContext) =>

{fooContext}

}
- {(barContext) =>

{barContext}

}
+

+ Hello, I am {fooValue} and I am {barValue} old +

); };