An open API service indexing awesome lists of open source software.

https://github.com/icyjoseph/context-hook-provider

A pattern proposal to move away from connect using hooks
https://github.com/icyjoseph/context-hook-provider

react-context react-hooks react-redux redux redux-provider

Last synced: about 1 month ago
JSON representation

A pattern proposal to move away from connect using hooks

Awesome Lists containing this project

README

          

# context-hook-provider

A pattern proposal.

The aim is to stop using connect. Instead, we should `useContext` and create hooks that select from the redux store.

Demo [here](https://nondescript-ship.surge.sh/).

## Install

As usual:

```bash
yarn add context-hook-provider
```

or

```bash
npm install context-hook-provider
```

## Usage

As usual, we create a redux store and pass it to the Provider.

```jsx
import React from "react";
import ReactDOM from "react-dom";
import { createStore } from "redux";
import { Provider } from "context-hook-provider";

// using createStore directly from redux
const store = createStore(reducer); // the regular redux store

ReactDOM.render(


,
document.getElementById("root")
);
```

Where Provider is:

```jsx
import React, { createContext, useState, useEffect } from "react";

// create a Context
export const State = createContext();

export function Provider({ store, children }) {
const [state, updateState] = useState(store.getState());

const listener = () => updateState(store.getState());

useEffect(() => {
const unsub = store.subscribe(listener);
return () => unsub();
}, []);

return (

{children}

);
}
```

Then we can easily consume the state. Say for example that ``, has a child called ``.

```jsx
import React, { useContext } from "react";
import { State } from "context-hook-provider";

export function Counter() {
const {
state: { count }
} = useContext(State);
return

{count}
;
}
```

Furthermore, you can make a hook on the `count`.

```jsx
import { useContext } from "react";

export function useCount() {
const { state } = useContext(State);
return state.count;
}
```

And re-do the Counter.

```jsx
import React from "react";
import useCount from "./";

export function Counter() {
const count = useCount();
return

{count}
;
}
```