大家好, 今天是我第一次去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;
根部减速器
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:“函数(){\ n返回dispatch(actionCreator.apply(this,arguments)); \ n}”
导航:对象{pop:“ function(){\ n var actionCreator = actionCreators [actionName]; \ n var action = actionCreator.apply(无效,0,参数); \ n返回navigation.dispatch(action); \ n}“,popToTop:”函数(){\ n var actionCreator = actionCreators [actionName]; \ n var action = actionCreator.apply(无效0,参数); \ n返回navigation.dispatch(action); \ n}“ ,推:“函数(){\ n var actionCreator = actionCreators [actionName]; \ n var action = actionCreator.apply(无效,0,参数); \ n返回navigation.dispatch(action); \ n}”,…}
screenProps:未定义
:对象{…} 305278fb-9c91-40bc-b0b1-9fa113a58b1f:93248:15
我希望这里的人发现什么是行不通的,我错过了什么,并为此提供帮助。 预先感谢您的时间和精力。
答案 0 :(得分:1)
您可能想通过在render
内记录道具来检查商店是否已更新。道具的任何变化都无法在您的构造函数中观察到。
props.fetchCompanies()
正在调用一个函数,该函数返回一个函数。该函数将传递给分派,然后被redux-thunk
拦截。
props.fetchCompanies()
只会调用外部函数,而不是内部函数。内部函数需要一个参数(dispatch
),该参数由redux-thunk提供。
const mapDispatchToProps = (dispatch) => {
return {
fetchCompanies: () => dispatch(fetchCompanies())
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Welcome);
注意
您不必如上所述手动添加调度,因为connect
会自动调度返回对象中的字段。
答案 1 :(得分:0)
我建议对操作进行一些细微改动。
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);
答案 2 :(得分:0)
您是否检查过actionTypes是否正确导入?
编辑: 我检查了您的reducer默认情况,即在情况FETCH_COMPANIES之前先调用它。不知道为什么要调用它,但是那样一来,您的状态就会被test属性覆盖。没有更多的values属性。然后在构造函数中调用fetchCompanies。它为您的州增添了价值。 mapStateToProps中有state.companies,但是您的州没有这种属性。尝试
values: state.values
,您将看到您的公司