Skip to content

Commit

Permalink
Prettier to api folder (reduxjs#2392)
Browse files Browse the repository at this point in the history
* Prettier to api folder

* Improve formatting

* Improve formatting
  • Loading branch information
CodinCat authored and timdorr committed May 12, 2017
1 parent 34cfc89 commit 9867ec5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 58 deletions.
11 changes: 8 additions & 3 deletions docs/api/Store.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ To learn how to describe asynchronous API calls, read the current state inside a

```js
import { createStore } from 'redux'
let store = createStore(todos, [ 'Use Redux' ])
let store = createStore(todos, ['Use Redux'])

function addTodo(text) {
return {
Expand Down Expand Up @@ -113,9 +113,14 @@ let currentValue
function handleChange() {
let previousValue = currentValue
currentValue = select(store.getState())

if (previousValue !== currentValue) {
console.log('Some deep nested property changed from', previousValue, 'to', currentValue)
console.log(
'Some deep nested property changed from',
previousValue,
'to',
currentValue
)
}
}

Expand Down
76 changes: 28 additions & 48 deletions docs/api/applyMiddleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { createStore, applyMiddleware } from 'redux'
import todos from './reducers'

function logger({ getState }) {
return (next) => (action) => {
return next => action => {
console.log('will dispatch', action)

// Call the next dispatch method in the middleware chain.
Expand All @@ -39,7 +39,7 @@ function logger({ getState }) {

let store = createStore(
todos,
[ 'Use Redux' ],
['Use Redux'],
applyMiddleware(logger)
)

Expand Down Expand Up @@ -70,7 +70,6 @@ function fetchSecretSauce() {
// These are the normal action creators you have seen so far.
// The actions they return can be dispatched without any middleware.
// However, they only express “facts” and not the “async flow”.

function makeASandwich(forPerson, secretSauce) {
return {
type: 'MAKE_SANDWICH',
Expand Down Expand Up @@ -104,13 +103,11 @@ store.dispatch(withdrawMoney(100))
// Meet thunks.
// A thunk is a function that returns a function.
// This is a thunk.

function makeASandwichWithSecretSauce(forPerson) {

// Invert control!
// Return a function that accepts `dispatch` so we can dispatch later.
// Thunk middleware knows how to turn thunk async actions into actions.

return function (dispatch) {
return fetchSecretSauce().then(
sauce => dispatch(makeASandwich(forPerson, sauce)),
Expand All @@ -121,52 +118,43 @@ function makeASandwichWithSecretSauce(forPerson) {

// Thunk middleware lets me dispatch thunk async actions
// as if they were actions!

store.dispatch(
makeASandwichWithSecretSauce('Me')
)
store.dispatch(makeASandwichWithSecretSauce('Me'))

// It even takes care to return the thunk's return value
// from the dispatch, so I can chain Promises as long as I return them.

store.dispatch(
makeASandwichWithSecretSauce('My wife')
).then(() => {
store.dispatch(makeASandwichWithSecretSauce('My wife')).then(() => {
console.log('Done!')
})

// In fact I can write action creators that dispatch
// actions and async actions from other action creators,
// and I can build my control flow with Promises.

function makeSandwichesForEverybody() {
return function (dispatch, getState) {
if (!getState().sandwiches.isShopOpen) {

// You don't have to return Promises, but it's a handy convention
// so the caller can always call .then() on async dispatch result.

return Promise.resolve()
}

// We can dispatch both plain object actions and other thunks,
// which lets us compose the asynchronous actions in a single flow.

return dispatch(
makeASandwichWithSecretSauce('My Grandma')
).then(() =>
Promise.all([
dispatch(makeASandwichWithSecretSauce('Me')),
dispatch(makeASandwichWithSecretSauce('My wife'))
])
).then(() =>
dispatch(makeASandwichWithSecretSauce('Our kids'))
).then(() =>
dispatch(getState().myMoney > 42 ?
withdrawMoney(42) :
apologize('Me', 'The Sandwich Shop')
return dispatch(makeASandwichWithSecretSauce('My Grandma'))
.then(() =>
Promise.all([
dispatch(makeASandwichWithSecretSauce('Me')),
dispatch(makeASandwichWithSecretSauce('My wife'))
])
)
.then(() => dispatch(makeASandwichWithSecretSauce('Our kids')))
.then(() =>
dispatch(
getState().myMoney > 42
? withdrawMoney(42)
: apologize('Me', 'The Sandwich Shop')
)
)
)
}
}

Expand All @@ -175,11 +163,9 @@ function makeSandwichesForEverybody() {

import { renderToString } from 'react-dom/server'

store.dispatch(
makeSandwichesForEverybody()
).then(() =>
response.send(renderToString(<MyApp store={store} />))
)
store
.dispatch(makeSandwichesForEverybody())
.then(() => response.send(renderToString(<MyApp store={store} />)))

// I can also dispatch a thunk async action from a component
// any time its props change to load the missing data.
Expand All @@ -189,16 +175,12 @@ import { Component } from 'react'

class SandwichShop extends Component {
componentDidMount() {
this.props.dispatch(
makeASandwichWithSecretSauce(this.props.forPerson)
)
this.props.dispatch(makeASandwichWithSecretSauce(this.props.forPerson))
}

componentWillReceiveProps(nextProps) {
if (nextProps.forPerson !== this.props.forPerson) {
this.props.dispatch(
makeASandwichWithSecretSauce(nextProps.forPerson)
)
this.props.dispatch(makeASandwichWithSecretSauce(nextProps.forPerson))
}
}

Expand All @@ -207,11 +189,9 @@ class SandwichShop extends Component {
}
}

export default connect(
state => ({
sandwiches: state.sandwiches
})
)(SandwichShop)
export default connect(state => ({
sandwiches: state.sandwiches
}))(SandwichShop)
```

#### Tips
Expand All @@ -223,11 +203,11 @@ export default connect(
* If you want to conditionally apply a middleware, make sure to only import it when it's needed:

```js
let middleware = [ a, b ]
let middleware = [a, b]
if (process.env.NODE_ENV !== 'production') {
let c = require('some-debug-middleware')
let d = require('another-debug-middleware')
middleware = [ ...middleware, c, d ]
middleware = [...middleware, c, d]
}

const store = createStore(
Expand Down
5 changes: 1 addition & 4 deletions docs/api/bindActionCreators.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ class TodoListContainer extends Component {
// removeTodo: Function
// }

return (
<TodoList todos={todos}
{...boundActionCreators} />
)
return <TodoList todos={todos} {...boundActionCreators} />

// An alternative to bindActionCreators is to pass
// just the dispatch function down, but then your child component
Expand Down
2 changes: 1 addition & 1 deletion docs/api/combineReducers.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ While `combineReducers` attempts to check that your reducers conform to some of
export default function todos(state = [], action) {
switch (action.type) {
case 'ADD_TODO':
return state.concat([ action.text ])
return state.concat([action.text])
default:
return state
}
Expand Down
4 changes: 2 additions & 2 deletions docs/api/createStore.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import { createStore } from 'redux'
function todos(state = [], action) {
switch (action.type) {
case 'ADD_TODO':
return state.concat([ action.text ])
return state.concat([action.text])
default:
return state
}
}

let store = createStore(todos, [ 'Use Redux' ])
let store = createStore(todos, ['Use Redux'])

store.dispatch({
type: 'ADD_TODO',
Expand Down

0 comments on commit 9867ec5

Please sign in to comment.