我遇到这个问题,正在调度操作,正在执行reducer但是没有触发render函数。
容器:
import React, { Component } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { renderVoterSearch } from '../actions';
import Search from '../components/Search';
class SearchContainer extends Component {
render() {
return (
<Search {...this.props}/>
);
}
}
const mapStateToProps = (state) => {
return {
searchType: state.searchType,
instruction: state.instruction,
title: state.title
}
}
const mapDispatchToProps = (dispatch) => {
return bindActionCreators({
renderVoterSearch
}, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(SearchContainer);
从搜索组件调度的操作
export const renderVoterSearch = (tab) => ({
type: RENDER_VOTER_SEARCH,
searchType: tab.type,
instruction: tab.instruction,
title: tab.title
});
减速机:
const search = (state = initialState, action) => {
switch (action.type) {
case RENDER_VOTER_SEARCH:
return {
...state,
searchType: action.searchType,
instruction: action.instruction,
title: action.title
}
default:
return state
}
}
这里是完整的代码 https://github.com/mivotico/mivotico-react-native/tree/redux-first-steps/app
我一直在读,其中一个原因可能是国家正在变异,但已经检查过,并没有看到任何突变。
提前致谢!
答案 0 :(得分:0)
发现问题!是不是我不知道reducer是在状态内,所以如果reducer被称为“搜索”,那么在mapStateToProps
函数中访问此状态应该是state.search
而不是state
}。