美好的一天大家,今天是我第一次去redux,我按照youtube教程让我的样板设置使用redux with 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;
根减速机
import { combineReducers } from 'redux';
import companyReducer from './companyReducer';
export default combineReducers({
companies: companyReducer
});
公司减速机
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);
这是我记录道具后在控制台上得到的:
{…}
公司:{...}
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.fetchCompanies()
正在调用一个返回函数的函数。此函数将被传递给调度,然后由redux-thunk
拦截。
props.fetchCompanies()
只会调用外部函数,而不是内部函数。内部函数需要一个参数(dispatch
),它由redux-thunk提供。
const mapDispatchToProps = (dispatch) => {
return {
fetchCompanies: () => dispatch(fetchCompanies())
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Welcome);
注意
您不必手动添加调度,如上所示,因为connect
会自动调度返回对象中的字段。
我想建议对行动进行微小的改动。
import {
FETCH_COMPANIES
} from './types';
export const fetchCompanies = payload => ({type: FETCH_COMPANIES, payload})
像更新组件
import {
connect
} from 'react-redux';
import { bindActionCreators } from "redux";
import { fetchCompanies } from '../../actions/companyActions';
const payload = [{
id: 1,
text: "CHRONUS FRANCE .A."
},
{
id: 2,
text: "two"
},
{
id: 3,
text: "three"
},
{
id: 4,
text: "four"
}]
//constructor
class Welcome extends Component {
constructor(props) {
super(props);
props.fetchCompanies(payload);
console.log(props);
}
}
//map to props and connect
const mapStateToProps = (state) => ({
companies: state.companies
});
const mapDispatchToProps = dispatch => bindActionCreators({fetchCompanies}, dispatch)
export default connect(mapStateToProps, mapDispatchToProps)(Welcome);
您是否检查过您的actionTypes是否正确导入?
编辑:我检查你的reducer默认情况是在案例FETCH_COMPANIES之前调用。不知道为什么会这样调用,但是你的状态会被test属性覆盖。没有更多的值属性。然后在构造函数中调用fetchCompanies。它为您的州增加了价值。 mapStateToProps中有state.companies但您所在的州没有这样的属性。尝试
values: state.values
,你会看到你的公司