Skip to content

Commit 8176ebf

Browse files
committed
First release
1 parent 5b936a0 commit 8176ebf

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

README.md

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
# react-extend-context
2-
Simple API for <i>extending</i> rather than <i>replacing</i> React.Context values
2+
Simple API for _extending_ rather than _replacing_ React.Context values. Lightweight, and has zero dependencies.
33

4+
## Why
5+
React provides the Context API to provide a React tree with props/state without having to rely on props drilling. This is great for single values, but is not as easy when dealing with multiple values. This package allows specifying _part_ of the value, and extends on top of any previous values from the tree.
46

57
## Installation
6-
78
This module is distributed via [npm][npm] and
89
should be installed as one of your project's regular `dependencies`:
910

1011
```
1112
yarn add react-extend-context
1213
```
13-
1414
or
15-
1615
```
1716
npm install react-extend-context
1817
```
@@ -24,32 +23,26 @@ The API for this package is meant to be simple and succinct. The only assumption
2423

2524
There are two exports for the package: the default `ReactExtendContext` component and a named Higher-Order Component `withExtendedContext` export.
2625

27-
2826
### ReactExtendContext example
2927
```jsx
3028
import React from 'react';
3129
import ReactExtendContext from 'react-extend-context';
3230

33-
// create a "CarContext" and provide some defaults
34-
const CarContext = React.createContext({
35-
make: '',
36-
model: '',
37-
type: '',
38-
year: 2000
39-
});
31+
// create a "CarContext"
32+
const CarContext = React.createContext( {} );
4033

41-
// Output the values from CarContext
34+
// Outputs the values from CarContext
4235
function Display() {
43-
const carContext = useContext(CarContext);
36+
const carContext = useContext( CarContext );
4437
const { make, model, type, year } = carContext;
4538

4639
return (<div>{year} {make} {model}: {type}</div>);
4740
}
4841

49-
// The expected outcome is "1997 Toyota Avalon: Coupe"
5042
export default function App() {
5143
return (
5244
<ReactExtendContext Context={CarContext} make="Toyota" model="Avalon" type="Coupe" year={1997}>
45+
{/* "1997 Toyota Avalon: Coupe" */}
5346
<Display />
5447
</ReactExtendContext>
5548
);
@@ -60,44 +53,51 @@ The above example is simple (and admittedly useless). The real use-case for this
6053

6154
You can pick and choose which Component adds (or modifies) which part of the context.
6255
```jsx
63-
// The expected outcome is "1997 Toyota Avalon: Coupe"
6456
export default function App() {
6557
return (
6658
<ReactExtendContext Context={CarContext} make="Toyota">
6759
<ReactExtendContext Context={CarContext} model="Avalon">
6860
<ReactExtendContext Context={CarContext} type="Coupe">
61+
{/* "1997 Toyota Avalon: Coupe" */}
6962
<ReactExtendContext Context={CarContext} year={1997}>
7063
<Display />
7164
</ReactExtendContext>
65+
66+
{/* "2020 Toyota Avalon: Coupe" */}
67+
<ReactExtendContext Context={CarContext} year={2020}>
68+
<Display />
69+
</ReactExtendContext>
7270
</ReactExtendContext>
7371
</ReactExtendContext>
7472
</ReactExtendContext>
7573
);
7674
}
7775
```
7876

79-
8077
### withExtendedContext HOC example
81-
The only other export from this package is the named export `withExtendedContext`. Generally, you don't deal with too many different types of React.Context, so this HOC provides a way to scope your context without having to constantly pass it down.
78+
The only other export from this package is the named export `withExtendedContext`. Generally, you don't deal with too many different types of React.Context, so this HOC provides a way to scope your context without having to pass it down at every level.
8279

8380
The signature that it expects is identical to ReactExtendContext, but you no longer have to pass Context at each level.
8481

85-
8682
```jsx
8783
import React from 'react';
8884
import { withExtendedContext } from 'react-extend-context';
8985

90-
...
9186
const ExtendedCarContext = withExtendedContext( CarContext );
92-
...
9387

94-
// The expected outcome is "1997 Toyota Avalon: Coupe"
88+
// The expected output is "1997 Toyota Avalon: Coupe"
9589
export default function App() {
9690
return (
9791
<ExtendedCarContext make="Toyota">
9892
<ExtendedCarContext model="Avalon">
9993
<ExtendedCarContext type="Coupe">
10094
<ExtendedCarContext year={1997}>
95+
{/* "1997 Toyota Avalon: Coupe" */}
96+
<Display />
97+
</ExtendedCarContext>
98+
99+
<ExtendedCarContext year={2020}>
100+
{/* "2020 Toyota Avalon: Coupe" */}
101101
<Display />
102102
</ExtendedCarContext>
103103
</ExtendedCarContext>
@@ -112,15 +112,13 @@ Similarly, you can mix and match which attributes are passed at which level:
112112
import React from 'react';
113113
import { withExtendedContext } from 'react-extend-context';
114114

115-
...
116115
const ExtendedCarContext = withExtendedContext( CarContext );
117-
...
118116

119-
// The expected outcome is "1997 Toyota Avalon: Coupe"
120117
export default function App() {
121118
return (
122119
<ExtendedCarContext make="Toyota" model="Avalon">
123120
<ExtendedCarContext type="Coupe" year={1997}>
121+
{/* "1997 Toyota Avalon: Coupe" */}
124122
<Display />
125123
</ExtendedCarContext>
126124
</ExtendedCarContext>
@@ -132,6 +130,5 @@ export default function App() {
132130
[MIT](LICENSE)
133131

134132
<!-- prettier-ignore-start -->
135-
[npm]: https://www.npmjs.com/
136-
[license]: https://github.com/testing-library/react-testing-library/blob/master/LICENSE
133+
[npm]: https://www.npmjs.com/package/react-extend-context
137134
<!-- prettier-ignore-end -->

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-extend-context",
3-
"version": "0.1.3",
3+
"version": "1.0.0",
44
"description": "Simple API for extending rather than replacing React.Context values",
55
"main": "dist/ReactExtendContext.js",
66
"typings": "dist/ReactExtendContext.d.ts",
@@ -20,7 +20,8 @@
2020
"keywords": [
2121
"react",
2222
"react.context",
23-
"context"
23+
"context",
24+
"typescript"
2425
],
2526
"license": "MIT",
2627
"repository": {

0 commit comments

Comments
 (0)