Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single loader for multiple components with HTTP requests

I created an LastUsers component and a LastPosts component. These components are reusable and have HTTP requests with API.

I have a page with these two components :

...
import LastUsers from './Module/LastUsers'
import LastPosts from './Module/LastPosts'

export default class Homepage extends Component {
    render() {
        return (
            <View>
                ...
                <LastUsers />
                <LastPosts />
                ...
            </View>
        )
    }
}

How to manage a common loader for these two (or more) components ?

I know handle a loader for a single component with ActivityIndicator and state :

...
constructor(props) {
    super(props)
    this.state = {
        ...
        isLoading: true,
    }
}

componentDidMount() {
    getQuery().then(data => {
        this.setState({
            ...
            isLoading: false,
        })
    })
}
...

But not a single loader for multiple components with HTTP requests

like image 763
Gaylord.P Avatar asked Jan 31 '26 18:01

Gaylord.P


1 Answers

inside components add this props :

for example inside LastUsers :

class LastUsers extends component {
 _req = ()=>{
   this.props.onStart();
   axios.get('http://...').then((res)=>{
      if(res.data === success){
         this.props.onSuccess()
      }
   })
 }
}


export default class Homepage extends Component {

_check = ()=>{
  if(this.state.lastUsers && this.state.lastPosts){
    loader : false
  }
}
render() {
    return (
        <View>
            ...
            <LastUsers onStart={()=>{this.setState(loader : true)}} onSuccess={()=>{
              this.setState({lastUsers : true})
              this._check()
            }/>
            <LastPosts onStart={()=>{this.setState(loader : true)}} onSuccess={()=>{
              this.setState({lastPosts: true})
              this._check()
            }/>
            ...
        </View>
    )
}


}
like image 89
Ehsan Hejazi Avatar answered Feb 03 '26 09:02

Ehsan Hejazi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!