【发布时间】:2020-03-04 23:14:43
【问题描述】:
我有 2 个减速器(计数和搜索),我已经组合了它们。当搜索组件调度时,两个 reducer 都会被调用。
- 这是调用所有减速器的预期行为吗?
- 或者,是否应该只调用最具体的 reducer(即搜索)?我必须怎么做才能只调用最具体的减速器?
这里是搜索、计数和组合减速器
const initialState = {url:'...'};
function search(state = initialState, action) {
if (action.type === 'SEARCH') {
...
return state;
}
return state;
}
export default search;
.
const initialState = 0;
function count(state = initialState, action) {
if (action.type === 'INCREMENT') {
return state + 1;
}
if (action.type === 'DECREMENT') {
return state - 1;
}
return state;
}
export default count;
.
import CountReducer from './CountReducer.js';
import SearchReducer from './ApiReducer.js';
import { combineReducers } from 'redux'
const reduce = combineReducers({
count: CountReducer,
search: SearchReducer
});
export default reduce;
搜索组件如下所示
import React, { Component } from 'react';
import { onSearch as onSearchAction } from '../store/actions/Actions.js';
import { connect } from 'react-redux';
class Search extends Component {
render() {
return (
<>
....
</>
);
}
}
const mapStateToProps = (state) => {
return { url: state.url };
}
const mapDispatchToProps = (dispatch, ownProps) => {
return { onSearch: () => dispatch(onSearchAction()) };
};
const ConnectedSearch = connect(mapStateToProps, mapDispatchToProps)(Search);
export default ConnectedSearch;
这里是搜索操作
export function onSearch() {
return {
type: "SEARCH",
url: '...'
}
}
【问题讨论】:
标签: reactjs redux react-redux