Open xgqfrms-GitHub opened 7 years ago
import ReduxThunk from 'redux-thunk';
http://redux.js.org/docs/api/applyMiddleware.html#example-custom-logger-middleware
import { createStore, applyMiddleware } from 'redux';
import todos from './reducers';
function logger({ getState }) {
return next => action => {
console.log('will dispatch', action);
// Call the next dispatch method in the middleware chain.
let returnValue = next(action);
console.log('state after dispatch', getState());
// This will likely be the action itself, unless
// a middleware further in chain changed it.
return returnValue;
};
}
let store = createStore(
todos,
['Use Redux'],
applyMiddleware(logger)
);
store.dispatch({
type: 'ADD_TODO',
text: 'Understand the middleware'
});
// (These lines will be logged by the middleware:)
// will dispatch: { type: 'ADD_TODO', text: 'Understand the middleware' }
// state after dispatch: [ 'Use Redux', 'Understand the middleware' ]
http://redux.js.org/docs/api/applyMiddleware.html#tips
If you want to conditionally apply a middleware, make sure to only import it when it's needed:
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];
}
const store = createStore(
reducer,
preloadedState,
applyMiddleware(...middleware)
);
Redux Thunk middleware allows you to write action creators that return a function instead of an action.
json
https://github.com/xgqfrms/React/commit/33a16aa8713da404e190162502830c0aee49dd33
https://gist.github.com/xgqfrms-GitHub/33b08ef2fe4705020af8a8d827775380#gistcomment-2150334
https://gist.github.com/xgqfrms-GitHub/33b08ef2fe4705020af8a8d827775380#gistcomment-2152052
https://gist.github.com/xgqfrms-GitHub/cbcf462a76a287e1bb3392f10f153fd4#gistcomment-2152001
https://gist.github.com/xgqfrms-GitHub/cbcf462a76a287e1bb3392f10f153fd4#gistcomment-2154144
pluralsight
json = response.json();
fetch(`https://api.github.com/users/xgqfrms/repos`)
.then(function(response) {
let json = response.json();
console.log(`response = ${json}`);
return json;
})
.then(function(json) {
console.log('parsed json: ', json)
});
fetch(`https://api.github.com/users/xgqfrms/repos`, {
method: 'get',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
.then(function(response) {
return response.json();
// json() ???
})
.then(function(json) {
console.log('parsed json: ', json)
})
.catch(function(error) {
console.log('parsing failed: ', error)
});
fetch(`https://api.github.com/users/xgqfrms/repos`)
.then(function(response) {
let json = response.json();
console.log(`response = ${json}`);
return json;
})
.then(function(json) {
console.log('parsed json: ', json);
console.log('parsed json: ', json[0]);
console.log('parsed json: ', json[1].name);
});
fetch(`https://api.github.com/users/xgqfrms/repos`)
.then(function(response) {
let json = response.json();
console.log(`response = ${json}`);
return json;
})
.then(function(json) {
console.log('parsed json: ', json);
console.log('parsed json: ', json[0].owner);
console.log('parsed json: ', json[1].name);
});
fetch(`https://api.github.com/users/xgqfrms/repos`, {
method: 'get',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
.then(function(response) {
return response.json();
// json() ???
})
.then(function(json) {
console.log('parsed json: ', json);
console.log('parsed json: ', json[0]);
console.log('parsed json: ', json[1].name);
})
.catch(function(error) {
console.log('parsing failed: ', error);
});
redux-thunk
http://redux.js.org/docs/api/applyMiddleware.html
https://github.com/gaearon/redux-thunk/blob/master/src/index.js
ES6, Error
ES5