Разделите actions
, reducers
и types
в свои собственные папки.
типы / index.js
export const SET_RANDOM_IMAGES = "SET_RANDOM_IMAGES";
действия / imageActions.js
import * as types from '../types';
export const getRandomImages = page => dispatch => (
fetch(`/boutiques/random-images/?page=${page}`)
.then(response => response.json())
.then(json => dispatch({ type: types.SET_RANDOM_IMAGES, payload: json.results })))
.catch(err => console.log(err))
)
Внутри компонента вы будете connect
переходить в состояние (state.images
или state.images.collection
) и dispatch
action
(getRandomImages
):
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { getRandomImages } from '../actions/imageActions';
class Example extends Component {
componentDidMount = () => this.props.getRandomImages(); // dispatches action creator
render = () => (
<div>
{ /*
The props below are redux state passed into
the component via the connect function
*/ }
{this.props.images.map(image => (
<img src={image.src} alt="image.name" />
))}
{this.props.collection.map(image => (
<img src={image.src} alt="image.name" />
))}
</div>
)
}
export default connect(state => ({
images: state.images, // pulled from redux state (state.images => this.props.images)
collection: state.images.collection // pulled from redux state (state.images.collection => this.props.images.collection)
}), { getRandomImages})(Example)
Затем он вызовет запрос AJAX
, а затем вернет type
и payload
вашему reducer
:
редукторы / index.js
import * as types from '../types'
// overwrite images for each successful fetch request
const imageReducer(state={}, {type, payload}) {
switch (type) {
// spread out any previous state, then spread out the payload (json.results)
case types.SET_RANDOM_IMAGES: return { ...state, ...payload }
default: return state;
}
}
// or append images on each successful fetch request...
const imageReducer(state={}, {type, payload}) {
switch (type) {
case types.SET_RANDOM_IMAGES:
return {
...state, // spread out any previous state
collection: [
...state.collection, // then spread out any previous "collection" state,
...payload // then spread/append the payload (json.results)
]
}
default: return state;
}
}
export default combineReducers({
images: imageReducer
});
Затем редуктор распространит любое предыдущее состояние imageReducer
, а затем добавит к нему res.results
через payload
. Теперь он существует в редуксе как state.images
или state.images.collection
. Затем он выводится из состояния избыточности и в компонент выше как this.props.images
или this.props.images.collection
.