Skip to content

Commit

Permalink
update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Pangallo, Antonio committed Jan 17, 2019
1 parent 5c9ca76 commit 584139c
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Getting Started with Redhooks

A tiny React utility library for holding a predictable state container in your React apps.
Inspired by https://redux.js.org, it uses the experimental Hooks API and the Context API.
Inspired by https://redux.js.org, it reimplements reduxjs concept using the experimental Hooks API and the Context API.

- [Motivation](#motivation)
- [Basic Example](#basic-example)
Expand Down Expand Up @@ -39,7 +39,8 @@ Redhooks follows the exact same principles of redux which is inspired to.
```js
import { createStore, combineReducers } from "redhooks";

const helloReducer = (
// function reducer
const hello = (
state = { phrase: "good morning" },
{ type, payload }
) => {
Expand All @@ -51,7 +52,8 @@ const helloReducer = (
}
};

const counterReducer = (state = 0, { type, payload }) => {
// function reducer
const counter = (state = 0, { type, payload }) => {
switch (type) {
case "INCREMENT":
return state + 1;
Expand All @@ -62,11 +64,11 @@ const counterReducer = (state = 0, { type, payload }) => {
}
};

const rootReducer = combineReducers({ helloReducer, counterReducer });
const rootReducer = combineReducers({ hello, counter });

const store = createStore(rootReducer);
// eventually we can pass to createStore as second arg an opts object like:
// const opts = { preloadedState: { counterReducer: 10 }, initialAction: { type: "INCREMENT" } }
// const opts = { preloadedState: { counter: 10 }, initialAction: { type: "INCREMENT" } }
// const store = createStore(rootReducer. opts);

export default store;
Expand Down Expand Up @@ -166,11 +168,11 @@ import { useStore } from "redhooks";

const ReadFromStore = () => {
const { state } = useStore();
const { helloReducer, counterReducer } = state;
const { hello, counter } = state;
return (
<section>
<h1>{helloReducer.phrase}</h1>
<span>{counterReducer}</span>
<h1>{hello.phrase}</h1>
<span>{counter}</span>
</section>
);
};
Expand All @@ -188,20 +190,20 @@ import { connect } from "redhooks";

class ReadFromStore extends Component {
render() {
const { helloReducer, counterReducer } = this.props;
const { hello, counter } = this.props;
return (
<section>
<h1>{helloReducer.phrase}</h1>
<span>{counterReducer}</span>
<h1>{hello.phrase}</h1>
<span>{counter}</span>
</section>
);
}
};

function mapStateToProp(state, prevState) {
return {
helloReducer: state.helloReducer,
counterReducer: state.counterReducer
hello: state.hello,
counter: state.counter
};
}

Expand Down Expand Up @@ -275,7 +277,7 @@ import rootReducer from "./reducers"
import App from './App'

const opts = {
preloadedState: { counterReducer: 9 },
preloadedState: { counter: 9 },
initialAction: { type: "INCREMENT" },
middlewares: [thunk]
};
Expand All @@ -293,7 +295,7 @@ import Provider, { createStore } from "redhooks";
import ReadFromStore from "./components/ReadFromStore";
import Footer from "./components/Footer";

const counterReducer = (state = 0, { type, payload }) => {
const counter = (state = 0, { type, payload }) => {
switch (type) {
case "INCREMENT":
return state + 1;
Expand All @@ -304,7 +306,7 @@ const counterReducer = (state = 0, { type, payload }) => {
}
};

const store = createStore(counterReducer);
const store = createStore(counter);

export default function SubApp() {
return (
Expand Down Expand Up @@ -374,7 +376,7 @@ combineReducers(reducers)

#### Example
```js
const rootReducer = combineReducers({ counterReducer, otherReducer })
const rootReducer = combineReducers({ counter, otherReducer })
const store = createStore(rootReducer)
```

Expand Down Expand Up @@ -444,17 +446,16 @@ const mapStateToProps = state => ({
counter: state.counter
});

// a verbose way
const mapDispatchToProps = dispatch => ({
actions: bindActionCreators(actions, dispatch)
});

export default connect(
mapStateToProps,
mapDispatchToProps
)(YourComponent);

// or simply

export default connect(
mapStateToProps,
{ actions }
Expand Down Expand Up @@ -496,10 +497,10 @@ import { useStore } from "redhooks";

const Example = () => {
const { state, dispatch } = useStore(); // do not use it within a Class Component
const { counterReducer } = state;
const { counter } = state;
return (
<section>
<span>{counterReducer}</span>
<span>{counter}</span>
<button onClick={() => dispatch({ type: "INCREMENT" })}>
Increment Counter
</button>
Expand Down

0 comments on commit 584139c

Please sign in to comment.