使用 Redux 处理数据
1. Redux 基础代码示例
首先,我们来看一段使用 Redux 处理数据的基础代码:
const { handleActions } = require('redux-actions')
const FETCH_MOVIES = 'movies/FETCH_MOVIES'
const FETCH_MOVIE = 'movies/FETCH_MOVIE'
const initialState = {
movies: [],
movie: {}
}
module.exports = {
fetchMoviesActionCreator: (movies) => ({
type: FETCH_MOVIES,
movies
}),
fetchMovieActionCreator: (index) => ({
type: FETCH_MOVIE,
index
}),
reducer: handleActions({
[FETCH_MOVIES]: (state, action) => ({
...state,
all: action.movies
}),
[FETCH_MOVIE]: (state, action) => ({
...state,
current: state.all[action.index - 1]
})
}, initialState)
}
上述代码定义了两个动作类型 F
超级会员免费看
订阅专栏 解锁全文
1237

被折叠的 条评论
为什么被折叠?



