Javascript 使用高阶组件显示加载程序
我可以使用每个容器中的条件显示加载程序图标,但当使用高阶组件时,我无法显示它。我已经在下面发布了我的代码。我使用了immutable.js,redux saga。下面是我编写的代码,它不起作用Javascript 使用高阶组件显示加载程序,javascript,reactjs,redux,higher-order-components,Javascript,Reactjs,Redux,Higher Order Components,我可以使用每个容器中的条件显示加载程序图标,但当使用高阶组件时,我无法显示它。我已经在下面发布了我的代码。我使用了immutable.js,redux saga。下面是我编写的代码,它不起作用 const mapStateToProps = createStructuredSelector({ logs: selectLogs(), logState: selectAllLogState(), #has requesting flag in it dialog: selectDial
const mapStateToProps = createStructuredSelector({
logs: selectLogs(),
logState: selectAllLogState(), #has requesting flag in it
dialog: selectDialog()
});
class Logs extends React.PureComponent {}
export default connect(mapStateToProps, mapDispatchToProps)(
Loader("requesting")(Logs)
);
export const selectAllLogState = () => state => state.get("logs");
export const selectLogs = () => state => state.getIn(["logs", "logs"]);
const Loader = prop => WrappedComponent => {
return class Loader extends React.Component {
render() {
return this.props[prop]
? <div className="earth-spinning">
<img
src={EarthSpinning}
alt="spinner"
style={{ margin: "0 auto" }}
/>
</div>
: <WrappedComponent {...this.props} />;
}
};
};
export default Loader;
const initialState = fromJS({
requesting: false,
deleted: false,
response: {},
logs: {},
error: null
});
function showLogs(state = initialState, action) {
switch (action.type) {
case LOGS_FETCH_REQUEST:
return state.set("requesting", true);
case LOGS_FETCH_SUCCESS:
return state
.set("requesting", false)
.set("logs", fromJS(action.logs.data.dataList, idReviver));
case LOGS_FETCH_FAILURE:
return state.set("error", action.error).set("requesting", false);
default:
return state;
}
}
export default showLogs;
const mapStateToProps=createStructuredSelector({
日志:选择日志(),
logState:selectAllLogState(),其中有请求标志
对话框:选择对话框()
});
类日志扩展了React.PureComponent{}
导出默认连接(MapStateTrops、mapDispatchToProps)(
加载器(“请求”)(日志)
);
export const selectAllLogState=()=>state=>state.get(“日志”);
export const selectLogs=()=>state=>state.getIn([“logs”,“logs”]);
常量加载器=prop=>WrappedComponent=>{
返回类加载器扩展了React.Component{
render(){
归还这个。道具[道具]
?
: ;
}
};
};
导出默认加载程序;
const initialState=fromJS({
请求:false,
删除:false,
答复:{},
日志:{},
错误:null
});
函数显示日志(状态=初始状态,操作){
开关(动作类型){
案例日志\u获取\u请求:
返回状态。设置(“请求”,true);
案例日志\u获取\u成功:
返回状态
.set(“请求”,false)
.set(“logs”,fromJS(action.logs.data.dataList,idrevor));
案例日志\u获取\u失败:
返回状态.set(“error”,action.error).set(“requising”,false);
违约:
返回状态;
}
}
导出默认显示日志;
很难说这段代码,你确定你的hoc从商店里得到了正确的道具吗?你可以像调用
createStructuredSelector({
…选择日志(),
日志状态:选择AllLogState(),
});代码>
或者明确设置此道具:
const mapStateToProps=createStructuredSelector({
日志:选择日志(),
正在请求:选择Logs()。正在请求
});代码>这里是要点,有完整的日志代码,减速机代码,选择器和加载器就我所知,您的请求的道具在日志中
输入这个。道具我如何将请求标志发送给加载器HOC,以便加载器也可以被其他组件使用,而不仅仅是日志组件?但是selectLogs只有logs对象,没有请求标志。我之所以只在选择器中获取logs对象,是因为其他标志(如response、error、deleted等)在任何时候都不需要OK,但您需要以某种方式从MapStateTrops
functionLoader(“logState”)(日志)中的存储中获取该请求的
值。是,因为当请求完成时,您正在更改日志中的请求
标志