Skip to content

Commit 6da70cb

Browse files
sag1vmarkerikson
authored andcommitted
website config and initial content (#50)
* website config and initial content * some references to react redux missed * prepare with format fixes * requested changes * header links and quick start md * Add new docs content pages * Fix doc buttons links * Update site config with docs pages * Tweak CSS * Remove unneeded docs files * Fix formatting * Stop failing builds for formatting issues
1 parent 30e1c59 commit 6da70cb

20 files changed

+10127
-1
lines changed

docs/api/configureStore.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
id: configureStore
3+
title: configureStore
4+
sidebar_label: configureStore
5+
hide_title: true
6+
---
7+
8+
# `configureStore`
9+
10+
A friendlier abstraction over the standard Redux `createStore` function. Takes a single configuration object parameter, with the following options:
11+
12+
```js
13+
function configureStore({
14+
// A single reducer function that will be used as the root reducer,
15+
// or an object of slice reducers that will be passed to combineReducers()
16+
reducer: Object<string, Function> | Function,
17+
// An array of Redux middlewares. If not supplied, defaults to just redux-thunk.
18+
middleware: Array<MiddlewareFunction>,
19+
// Built-in support for devtools. Defaults to true.
20+
devTools: boolean,
21+
// Same as current createStore.
22+
preloadedState : State,
23+
// Same as current createStore.
24+
enhancer : ReduxStoreEnhancer,
25+
})
26+
```
27+
28+
Basic usage:
29+
30+
```js
31+
import { configureStore } from 'redux-starter-kit'
32+
33+
import rootReducer from './reducers'
34+
35+
const store = configureStore({ reducer: rootReducer })
36+
// The store now has redux-thunk added and the Redux DevTools Extension is turned on
37+
```
38+
39+
Full example:
40+
41+
```js
42+
import { configureStore, getDefaultMiddleware } from 'redux-starter-kit'
43+
44+
// We'll use redux-logger just as an example of adding another middleware
45+
import logger from 'redux-logger'
46+
47+
// And use redux-batch as an example of adding enhancers
48+
import { reduxBatch } from '@manaflair/redux-batch'
49+
50+
import todosReducer from './todos/todosReducer'
51+
import visibilityReducer from './visibility/visibilityReducer'
52+
53+
const reducer = {
54+
todos: todosReducer,
55+
visibility: visibilityReducer
56+
}
57+
58+
const middleware = [...getDefaultMiddleware(), logger]
59+
60+
const preloadedState = {
61+
todos: [
62+
{
63+
text: 'Eat food',
64+
completed: true
65+
},
66+
{
67+
text: 'Exercise',
68+
completed: false
69+
}
70+
],
71+
visibilityFilter: 'SHOW_COMPLETED'
72+
}
73+
74+
const store = configureStore({
75+
reducer,
76+
middleware,
77+
devTools: NODE_ENV !== 'production',
78+
preloadedState,
79+
enhancers: [reduxBatch]
80+
})
81+
82+
// The store has been created with these options:
83+
// - The slice reducers were automatically passed to combineReducers()
84+
// - redux-thunk and redux-logger were added as middleware
85+
// - The Redux DevTools Extension is disabled for production
86+
// - The middleware, batch, and devtools enhancers were automatically composed together
87+
```

docs/api/createAction.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
id: createAction
3+
title: createAction
4+
sidebar_label: createAction
5+
hide_title: true
6+
---
7+
8+
# `createAction`
9+
10+
A utility function to create an action creator for the given action type string. The action creator accepts a single argument, which will be included in the action object as a field called `payload`. The action creator function will also have its `toString()` overriden so that it returns the action type, allowing it to be used in reducer logic that is looking for that action type.
11+
12+
```js
13+
// actions.js
14+
import { createAction } from 'redux-starter-kit'
15+
16+
export const increment = createAction('increment')
17+
18+
console.log(increment)
19+
// "increment"
20+
21+
const theAction = increment(5)
22+
console.log(theAction)
23+
// {type : "increment", payload : 5}
24+
25+
// reducer.js
26+
import { increment } from './actions'
27+
28+
function counterReducer(state = 0, action) {
29+
switch (action.type) {
30+
// action creator can be used directly as the type for comparisons
31+
case increment: {
32+
return state + action.payload
33+
}
34+
default:
35+
return state
36+
}
37+
}
38+
```
39+
40+
Since action creators returned by `createAction` have `toString()` overridden, they can be used in `createReducer` as a key in the `actionsMap`:
41+
42+
```js
43+
// reducer.js
44+
import { createReducer } from 'redux-starter-kit'
45+
import { increment } from './actions'
46+
47+
const counterReducer = createReducer(0, {
48+
[increment]: (state, action) => state + action.payload
49+
})
50+
```

docs/api/createReducer.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
id: createReducer
3+
title: createReducer
4+
sidebar_label: createReducer
5+
hide_title: true
6+
---
7+
8+
# `createReducer`
9+
10+
A utility function to create reducers that handle specific action types, similar to the example function in the ["Reducing Boilerplate" Redux docs page](https://redux.js.org/recipes/reducing-boilerplate#generating-reducers). Takes an initial state value and an object that maps action types to case reducer functions. Internally, it uses the [`immer` library](https://github.com/mweststrate/immer), so you can write code in your case reducers that mutates the existing `state` value, and it will correctly generate immutably-updated state values instead.
11+
12+
```js
13+
function createReducer(
14+
initialState: State,
15+
actionsMap: Object<String, Function>
16+
) {}
17+
```
18+
19+
Example usage:
20+
21+
```js
22+
import { createReducer } from 'redux-starter-kit'
23+
24+
function addTodo(state, action) {
25+
const { newTodo } = action.payload
26+
27+
// Can safely call state.push() here
28+
state.push({ ...newTodo, completed: false })
29+
}
30+
31+
function toggleTodo(state, action) {
32+
const { index } = action.payload
33+
34+
const todo = state[index]
35+
// Can directly modify the todo object
36+
todo.completed = !todo.completed
37+
}
38+
39+
const todosReducer = createReducer([], {
40+
ADD_TODO: addTodo,
41+
TOGGLE_TODO: toggleTodo
42+
})
43+
```
44+
45+
This doesn't mean that you _have to_ write code in your case reducers that mutates the existing `state` value, you can still write code that updates the state immutably. You can think of `immer` as a safety net, if the code is written in a way that mutates the state directly, `immer` will make sure that such update happens immutably. On the other hand the following code is still valid:
46+
47+
```js
48+
import { createReducer } from 'redux-starter-kit'
49+
50+
function addTodo(state, action) {
51+
const { newTodo } = action.payload
52+
53+
// Updates the state immutably without relying on immer
54+
return [...state, { ...newTodo, completed: false }]
55+
}
56+
57+
function toggleTodo(state, action) {
58+
const { index } = action.payload
59+
60+
const todo = state[index]
61+
// Updates the todo object immutably withot relying on immer
62+
return state.map((todo, i) => {
63+
if (i !== index) return todo
64+
return { ...todo, completed: !todo.completed }
65+
})
66+
}
67+
68+
const todosReducer = createReducer([], {
69+
ADD_TODO: addTodo,
70+
TOGGLE_TODO: toggleTodo
71+
})
72+
```

docs/api/createSelector.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
id: createSelector
3+
title: createSelector
4+
sidebar_label: createSelector
5+
hide_title: true
6+
---
7+
8+
# `createSelector`
9+
10+
The `createSelector` utility from the [`selectorator` library](https://github.com/planttheidea/selectorator), re-exported for ease of use. It acts as a superset of the standard `createSelector` function from [Reselect](https://github.com/reactjs/reselect). The primary improvements are the ability to define "input selectors" using string keypaths, or return an object result based on an object of keypaths. It also accepts an object of customization options for more specific use cases.
11+
12+
For more specifics, see the [`selectorator` usage documentation](https://github.com/planttheidea/selectorator#usage).
13+
14+
```js
15+
function createSelector(
16+
// Can either be:
17+
// - An array containing selector functions, string keypaths, and argument objects
18+
// - An object whose keys are selector functions and string keypaths
19+
paths : Array<Function | string | Object> | Object<string, string | Function>
20+
)
21+
```
22+
23+
Example usage:
24+
25+
```js
26+
// Define input selector using a string keypath
27+
const getSubtotal = createSelector(['shop.items'], items => {
28+
// return value here
29+
})
30+
31+
// Define input selectors as a mix of other selectors and string keypaths
32+
const getTax = createSelector(
33+
[getSubtotal, 'shop.taxPercent'],
34+
(subtotal, taxPercent) => {
35+
// return value here
36+
}
37+
)
38+
39+
// Define input selector using a custom argument index to access a prop
40+
const getTabContent = createSelector(
41+
[{ path: 'tabIndex', argIndex: 1 }],
42+
tabIndex => {
43+
// return value here
44+
}
45+
)
46+
47+
const getContents = createSelector({ foo: 'foo', bar: 'nested.bar' })
48+
// Returns an object like: {foo, bar}
49+
```

docs/api/createSlice.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
id: createSlice
3+
title: createSlice
4+
sidebar_label: createSlice
5+
hide_title: true
6+
---
7+
8+
# `createSlice`
9+
10+
A function that accepts an initial state, an object full of reducer functions, and optionally a "slice name", and automatically generates action creators, action types, and selectors that correspond to the reducers and state.
11+
12+
The reducers will be wrapped in the [`createReducer()` utility](createReducer.md), and so they can safely "mutate" the state they are given.
13+
14+
```js
15+
import { createSlice } from 'redux-starter-kit'
16+
import { createStore, combineReducers } from 'redux'
17+
18+
const counter = createSlice({
19+
slice: 'counter', // slice is optional, and could be blank ''
20+
initialState: 0,
21+
reducers: {
22+
increment: state => state + 1,
23+
decrement: state => state - 1,
24+
multiply: (state, action) => state * action.payload
25+
}
26+
})
27+
28+
const user = createSlice({
29+
slice: 'user',
30+
initialState: { name: '' },
31+
reducers: {
32+
setUserName: (state, action) => {
33+
state.name = action.payload // mutate the state all you want with immer
34+
}
35+
}
36+
})
37+
38+
const reducer = combineReducers({
39+
counter: counter.reducer,
40+
user: user.reducer
41+
})
42+
43+
const store = createStore(reducer)
44+
45+
store.dispatch(counter.actions.increment())
46+
// -> { counter: 1, user: {} }
47+
store.dispatch(counter.actions.increment())
48+
// -> { counter: 2, user: {} }
49+
store.dispatch(counter.actions.multiply(3))
50+
// -> { counter: 6, user: {} }
51+
console.log(`${counter.actions.decrement}`)
52+
// -> counter/decrement
53+
store.dispatch(user.actions.setUserName('eric'))
54+
// -> { counter: 6, user: { name: 'eric' } }
55+
const state = store.getState()
56+
console.log(user.selectors.getUser(state))
57+
// -> { name: 'eric' }
58+
console.log(counter.selectors.getCounter(state))
59+
// -> 6
60+
```

docs/api/getDefaultMiddleware.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
id: getDefaultMiddleware
3+
title: getDefaultMiddleware
4+
sidebar_label: getDefaultMiddleware
5+
hide_title: true
6+
---
7+
8+
# `getDefaultMiddleware`
9+
10+
`getDefaultMiddleware` is useful if you need to add custom middlewares without removing `redux-starter-kit`'s default middleware.
11+
12+
Currently it returns an array with `redux-thunk`.

docs/api/otherExports.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
id: other-exports
3+
title: Other Exports
4+
sidebar_label: Other Exports
5+
hide_title: true
6+
---
7+
8+
# Other Exports
9+
10+
`redux-starter-kit` re-exports additional functions from other dependencies as well.
11+
12+
## `createNextState`
13+
14+
The default immutable update function from the [`immer` library](https://github.com/mweststrate/immer#api), re-exported here as `createNextState` (also commonly referred to as `produce`)
15+
16+
## `combineReducers`
17+
18+
Redux's `combineReducers`, re-exported for convenience. While `configureStore` calls this internally, you may wish to call it yourself to compose multiple levels of slice reducers.
19+
20+
## `compose`
21+
22+
Redux's `compose`. It composes functions from right to left.
23+
This is a functional programming utility. You might want to use it to apply several store custom enhancers/ functions in a row.

0 commit comments

Comments
 (0)