Skip to content

Commit

Permalink
feat: performance comparisson regular Contxt
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-marcelo-gallardo committed Mar 6, 2020
1 parent d3f268c commit df26399
Show file tree
Hide file tree
Showing 27 changed files with 24,576 additions and 34 deletions.
72 changes: 39 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
- [Selections using Object](#selections-using-object)
- [Selections using Function](#selections-using-function)
- [Other Use-cases](#other-use-cases)
- [Fetching initial data](#fetching-initial-data)
- [Combining Contexts](#combining-contexts)
- [Merged Store](#merged-store)
- [Deriving State](#deriving-state)
- [Fetching initial data](#fetching-initial-data)
- [Store without reducer](#store-without-reducer)
- [Benefits of using Context](#benefits-of-using-context)
- [Problems or Suggestions](#problems-or-suggestions)
Expand Down Expand Up @@ -350,6 +350,36 @@ Lastly, you can specify a `Function` to create the resulting object from the `st

## Other Use-cases

### Fetching initial data

Sometimes you'll need to call some service when the application starts so you can populate your store with initial data.

To cover that scenario, the `Provider` returned by `createContextProvider` accepts a special property called `onInit`, which expects to receive a tuple (array) with two values: a selections object and a function.

The `selection object` is the same as the one used with connect HOC and allows selecting only what you need from your store, as well as applying derived state functions.

The `function` provided as second parameter will be called with the result of the previous selection.

Note that, if provided, `onInit` function will be triggered only once when the Provider is first rendered; e.g.:

```js
// index.tsx

const selectionOption = {
actions: ['fetchTodos'],
}

const onInit({ fetchTodos }) => fetchTodos();

render(
<TodosProvider onInit={[selectionOption, onInit]}>
<App />
</TodosProvider>
document.getElementById('root')
)

```

### Combining Contexts

There are scenarios in which you'll need to access more than one `Context` to gather all the values your component needs. In such cases you can use `mergedConnectContextFactory` helper function; e.g.:
Expand Down Expand Up @@ -484,36 +514,6 @@ The `computedSelectors` option expects an object with the following signature:
**Important:** `computedSelectors` are memoized using `React.memo()`; this avoids re-computing a giving selector if none of the listed dependencies changed.


### Fetching initial data

Sometimes you'll need to call some service when the application starts so you can populate your store with initial data.

To cover that scenario, the `Provider` returned by `createContextProvider` accepts a special property called `onInit`, which expects to receive a tuple (array) with two values: a selections object and a function.

The `selection object` is the same as the one used with connect HOC and allows selecting only what you need from your store, as well as applying derived state functions.

The `function` provided as second parameter will be called with the result of the previous selection.

Note that, if provided, `onInit` function will be triggered only once when the Provider is first rendered; e.g.:

```js
// index.tsx

const selectionOption = {
actions: ['fetchTodos'],
}

const onInit({ fetchTodos }) => fetchTodos();

render(
<TodosProvider onInit={[selectionOption, onInit]}>
<App />
</TodosProvider>
document.getElementById('root')
)

```

### Store without reducer

There are cases in which you'll need to add some values to a Context to make them available to every component, but those values won't change so often, for example, to keep track of authentication. In those cases, creating a `reducer` might be a little overkill, so you can skip it and use the library as follows:
Expand All @@ -529,9 +529,14 @@ import authService from './authService';
const loginAction = (dispatch, getState) => async (username, password) => {
dispatch({ loading: true });

const user = await authService.login(username, password);
try {
const user = await authService.login(username, password);

dispatch({ loading: false, user, isAuthenticated: false });
dispatch({ loading: false, user, isAuthenticated: true });

} catch (error) {
dispatch({ loading: false, error });
}
}

const actions = {
Expand All @@ -542,6 +547,7 @@ const initialState = {
isAuthenticated: false,
user: {},
loading: false,
error: null,
}

const [AuthProvider, AuthContext] = createContextProvider(initialState, actions);
Expand Down
23 changes: 23 additions & 0 deletions examples/compare-context/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
68 changes: 68 additions & 0 deletions examples/compare-context/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `yarn start`

Runs the app in the development mode.<br />
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br />
You will also see any lint errors in the console.

### `yarn test`

Launches the test runner in the interactive watch mode.<br />
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `yarn build`

Builds the app for production to the `build` folder.<br />
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br />
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `yarn eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting

### Analyzing the Bundle Size

This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size

### Making a Progressive Web App

This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app

### Advanced Configuration

This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration

### Deployment

This section has moved here: https://facebook.github.io/create-react-app/docs/deployment

### `yarn build` fails to minify

This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
Loading

0 comments on commit df26399

Please sign in to comment.