redux - Redux mapStateToProps() 在使用 Next.js 返回后不呈现
问题描述
我一直在努力弄清楚为什么render()
不被调用 after mapStateToProps()
。
目前,工作秩序是
getInitialProps
->mapStateToProps
->mapDispatchToProps
->constructor
->render
->componentDidMount(here dispatch with Redux)
->mapStateToProps
-> 结束
就像您看到的顺序一样,应该在调度发生之后调用 render(),以便使用来自 Redux 的调度数据呈现页面。
有谁知道为什么?
页面/搜索/index.js
import { connect } from "react-redux";
import Container from "./container";
import { actionCreators as storeActions } from "lib/modules/stores";
const mapStateToProps = (state, ownProps) => {
const { storeList } = state;
return { storeList };
};
const mapDispatchToProps = (dispatch, ownProps) => {
const { searchTerm } = ownProps;
return {
searchByTerm: pageNum => {
dispatch(storeActions.searchByTerm(searchTerm, pageNum));
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(Container);
页面/搜索/container.js
import Search from "./presenter";
class Index extends React.Component {
static async getInitialProps(context) {
const { searchTerm } = context.query;
return {
searchTerm
};
}
state = {
loading: true,
offset: 0,
page: 1,
limit: 10,
nlabel: ">>",
plabel: "<<"
};
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
componentDidMount() {
const { searchByTerm } = this.props;
const { page } = this.state;
searchByTerm(page);
}
render() {
const { classes, storeList, goToSearch, searchTerm } = this.props;
return (
<Search
{...this.state}
storeList={storeList}
searchTerm={searchTerm}
/>
);
}
}
export default Index;
解决方案
推荐阅读
- automake - 如何从 automake/libtool 库中获取符号列表?
- python - AES - 如何将字节字符串转换为字节字节字符串?
- asp.net-core - 带有 FromBody 的路由不适用于 ASP.NET Core
- arrays - 尝试从 SwiftUI 视图中的嵌套数组调用
- javascript - 为我的原型使用“Swiper”JS 库时的 CSS 挑战
- scala - 在 Scala 中,是否可以通过迭代 ListMap 将部分函数的集合构建成一个函数?
- firefox - Chart.js 的跨浏览器支持
- jquery - Django:模型和视图以及 jQuery DataTables,hello world 示例
- firebase - 重新导航后重新创建流
- docker - Docker 列表容器 - Gitlab Runner