大家好, 今天是我第一次尝试 Redux,我按照 YouTube 教程进行了样板设置,以将 Redux 与 React-Native 结合使用,显然,我面临着一些问题,并且看不到我可能犯的错误,因为这个概念仍然存在对我来说有点模糊。
所以我创建了一个简化的操作和类型作为起点。问题是在将状态连接到组件并触发组件构造函数中的操作之后,我从操作中获得了结果(使用控制台日志进行了测试),但状态并未更改。
这是我的代码:
商店
import {
createStore,
applyMiddleware
} from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './src/reducers/rootReducer';
const initialState = {};
const middleware = [thunk];
const store = createStore(rootReducer, initialState, applyMiddleware(...middleware));
export default store;
根 reducer
import { combineReducers } from 'redux';
import companyReducer from './companyReducer';
export default combineReducers({
companies: companyReducer
});
公司 reducer
import { FETCH_COMPANIES } from '../actions/types';
const initialState = {
values: []
};
export default (state = initialState, action) => {
switch(action.type) {
case FETCH_COMPANIES: return {
...state,
values: action.payload
};
default: return {
test: 'testing'
};
}
}
公司行动
import {
FETCH_COMPANIES
} from './types';
export const fetchCompanies = () => dispatch =>
dispatch({
type: FETCH_COMPANIES,
payload: [{
id: 1,
text: "CHRONUS FRANCE .A."
},
{
id: 2,
text: "two"
},
{
id: 3,
text: "three"
},
{
id: 4,
text: "four"
},
{
id: 5,
text: "five"
},
{
id: 6,
text: "six"
},
{
id: 7,
text: "seven"
},
{
id: 8,
text: "eight"
},
{
id: 9,
text: "nine"
},
{
id: 10,
text: "ten"
},
]
});
类型
export const FETCH_COMPANIES = 'FETCH_COMPANIES';
组件
//imports
import {
connect
} from 'react-redux';
import { fetchCompanies } from '../../actions/companyActions';
//constructor
class Welcome extends Component {
constructor(props) {
super(props);
props.fetchCompanies();
console.log(props);
}
}
//map to props and connect
const mapStateToProps = (state) => ({
companies: state.companies
});
export default connect(mapStateToProps, { fetchCompanies })(Welcome);
这是我在记录 Prop 后在控制台上得到的结果:
{…}
companies: {…}
test: "testing" <prototype>: Object { … }
fetchCompanies: "function () {\n return dispatch(actionCreator.apply(this, arguments));\n }"
navigation: Object { pop: "function () {\n var actionCreator = actionCreators[actionName];\n var action = actionCreator.apply(void 0, arguments);\n return navigation.dispatch(action);\n }", popToTop: "function () {\n var actionCreator = actionCreators[actionName];\n var action = actionCreator.apply(void 0, arguments);\n return navigation.dispatch(action);\n }", push: "function () {\n var actionCreator = actionCreators[actionName];\n var action = actionCreator.apply(void 0, arguments);\n return navigation.dispatch(action);\n }", … }
screenProps: undefined
: Object { … } 305278fb-9c91-40bc-b0b1-9fa113a58b1f:93248:15
我希望这里有人能发现什么不起作用以及我错过了什么并帮助我解决这个问题。 预先感谢您的时间和精力。
最佳答案
您可能想通过在 render
中记录 props 来检查商店是否已更新。在构造函数中无法观察到 props 的任何更改。
props.fetchCompanies()
正在调用一个返回函数的函数。该函数将被传递给dispatch,然后被redux-thunk拦截。
props.fetchCompanies()
只会调用外部函数,但不会调用内部函数。内部函数需要一个参数(dispatch
),该参数由 redux-thunk 提供。
const mapDispatchToProps = (dispatch) => {
return {
fetchCompanies: () => dispatch(fetchCompanies())
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Welcome);
注意
您不必如上所示手动添加调度,因为 connect
会自动调度返回对象中的字段。
关于reactjs - redux reducer 未收到操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55650494/