Reactjs 在React Redux中MapStateTops之前对操作进行分段
我正在react中开发一个userProfile容器组件,它在从服务器对数据进行fet后显示一些数据 这是一个组件:Reactjs 在React Redux中MapStateTops之前对操作进行分段,reactjs,react-redux,redux-saga,Reactjs,React Redux,Redux Saga,我正在react中开发一个userProfile容器组件,它在从服务器对数据进行fet后显示一些数据 这是一个组件: import React from 'react'; import {connect} from 'react-redux'; import PersonalInformationView from './PersonalInformationView'; import * as selectors from '../../../reducers'; class Per
import React from 'react';
import {connect} from 'react-redux';
import PersonalInformationView from './PersonalInformationView';
import * as selectors from '../../../reducers';
class PersonalInformationViewContainer extends React.Component{
constructor(props, context){
super(props, context);
}
render(){
return (
<PersonalInformationView userProfile={this.props.userProfile}/>
);
}
}
const mapStateToProps = (state, ownProps) => {
return {
user: selectors.getUser(state),
userProfile: selectors.getUserProfile(state)
};
};
const mapDispatchToProps = () =>{
return {
};
};
export default connect(mapStateToProps, mapDispatchToProps)(PersonalInformationViewContainer);
从“React”导入React;
从'react redux'导入{connect};
从“./PersonalInformationView”导入PersonalInformationView;
从“../../../reducers”导入*作为选择器;
类PersonalInformationViewContainer扩展了React.Component{
构造函数(道具、上下文){
超级(道具、背景);
}
render(){
返回(
);
}
}
const mapStateToProps=(state,ownProps)=>{
返回{
用户:选择器。getUser(状态),
userProfile:selectors.getUserProfile(状态)
};
};
const mapDispatchToProps=()=>{
返回{
};
};
导出默认连接(mapStateToProps、mapDispatchToProps)(PersonalInformationViewContainer);
我遇到的问题是,我需要对调用传奇并带来数据的动作FETCH\u USER\u PROFILE\u start
。这需要在调用选择器getUserProfile
之前完成,因为上面的代码会导致我得到一个未定义的结果
什么是确保我已完成数据提取的最佳方法,以便在我的组件中不会未定义props.userProfile?您只需添加
if (!this.props.userProfile)
return null;
在渲染方法的开头,使组件渲染为空
或在等待获取时渲染正在加载的文本或动画,而不是null
或者根本不渲染组件,将测试放在组件层次结构的更高位置
从技术上讲,您可以避免在提取开始之前调用getUserProfile
选择器,但这将导致不必要的复杂、丑陋、不可维护和不可读的代码。。。你为什么要那样做
实际上,希望调用选择器,并且希望它返回
未定义的
,这是一个简单的事实 谢谢你的回答。还有一个问题。我应该在哪里调用dispatch来获取用户配置文件?例如,您可以使用组件didmount
,但这取决于您的需要。