【发布时间】:2020-03-16 02:38:14
【问题描述】:
我正在处理大型 reactjs 应用程序,在该应用程序中,有很多组件,也有很多 api 服务。 我的问题是如何防止在不同的组件中调用相同的 api。 实际上我想调用一次 api,我将使用该 api 响应整个应用程序,这样我们就可以防止在不同的组件中调用相同的 api。 所以请给我任何解决方案。 查看下面的演示代码:
enter code here
`FirstComponent:
——————————————
Class FirstComponent extends Component {
constructor(props){
this.setState={}
}
componentDidMount() {
this.props.dispatch(getById(1)); //here first time I am calling api
}
render(){
return(
———HTML Code HERE———
)
}
const mapStateToProps = state => {
return {
registrationData: state.RegistrationDemand.registrationData
// here I am getting response through redux reducer store
};
};
export default compose(
translate,
withRouter,
connect(mapStateToProps)
)(FirstComponent);
SecondComponent:
——————————————
Class SecondComponent extends Component {
constructor(props){
this.setState={}
}
componentDidMount() {
this.props.dispatch(getById(1)); //here I need to prevent this second time api calling
}
render(){
return(
———HTML Code HERE———
)
}
const mapStateToProps = state => {
return {
registrationData: state.RegistrationDemand.registrationData
//without calling second component,If I use this one first time when I redirect to this page data is coming hereabout when I refresh second time it is getting null.
};
};
export default compose(
translate,
withRouter,
connect(mapStateToProps)
)(SecondComponent);`
【问题讨论】:
标签: reactjs react-redux