Save previous view when move data fetch from componentWillUpdate(WillReceiveProps) to ComponentDidUpdate
Personne n'a encore pris cette issue.
- Langage dominant
- JavaScript
- Étoiles
- 11.8k
- Forks
- 7.9k
- Merge moyen
- 1 j 11 h
- PR mergées (30 j)
- 11
Description
Hi. everyone. Trying to move my fetching for component frome Legasy to recommended lifecycle methods.
In this post we loading component when data is fetched, otherwise we show loader. I need to fetch all data to show component, and while fetching need show not only loader, but loader up on my current view(component). Before i did it in
componentWillReceiveProps(nextProps){
const current = `${this.props.location.pathname}${this.props.location.search}`
const next = `${nextProps.location.pathname}${nextProps.location.search}`
if (current === next && this.props.isAuth === nextProps.isAuth) {
return
}
this.fetchRoutes(nextProps)
}
shouldComponentUpdate(nextProps, nextState) {
return !nextState.isAppFetching && (!isEqual(this.state, nextState) || !isEqual(this.props, nextProps)
}
fetchRoutes(nextProps) {
const {dispatch, location} = nextProps
dispatch(showLoading())
this.setState({
isAppFetching : true
}, () =>
reactRouterFetch( routes, location, { dispatch }).then(
() => this.setState({isAppFetching: false}, () => dispatch(hideLoading())),
() => this.setState({isAppFetching: false}, () => dispatch(hideLoading()))
)(this is ajax call)
)
}
render () {
const { appFetchingError} = this.props
const landingPage = this.state.isAppFetching ? null : appFetchingError.status
? handlingApiError(appFetchingError, this.props)
: render UI with all props from redux
.....
}
so if i change pathname i start fetching my data. Then it avoid render while fetching data in shouldComponentUpdate. So by this i show my previous route component and loading bar while fetching, then change my view to new UI according to new route.
By moving WillReceiveProps internal code to DidUpdate, first of all it change my view then show loadingBar up on It then fullfill new view by fetched props.
How can i avoid view changing untill all data wii be fetched from server (like GitHub links jumping)? is it possible to save previous rendered things with previousProps and previousState?
New code is
`componentDidUpdate(prevProps){
const current = `${this.props.location.pathname}${this.props.location.search}`
const prev = `${prevProps.location.pathname}${prevProps.location.search}`
if (current === prev && this.props.isAuth === prevProps.isAuth) {
return
}
if(!prevProps.manual) this.fetchRoutes(this.props)
}
shouldComponentUpdate(nextProps, nextState) {
return (nextState.isDataFetched || nextState.needDataFetch) && (!isEqual(this.state, nextState) || !isEqual(this.props, nextProps) )
}
fetchRoutes(nextProps) {
const {dispatch, location} = nextProps
dispatch(showLoading())
this.setState({
isDataFetched : false,
needDataFetch: true
}, () =>
reactRouterFetch( routes, location, { dispatch })
.then(() => dispatch(hideLoading()),
() => dispatch(hideLoading()))
.then(() => this.setState({isDataFetched: true, needDataFetch: false}))
)
}
render () {
const { appFetchingError } = this.props
const landingPage = !this.state.isDataFetched ? null : appFetchingError.status
? handlingApiError(appFetchingError, this.props)
: render UI with all props.
......
}
And my screen will show loadingBar on empty page while fetching according to null in render condition, instead previous view
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
L’issue fournit des exemples de cycle de vie utilisant componentWillReceiveProps, componentDidUpdate, shouldComponentUpdate et fetchRoutes, mais ne nomme aucun fichier du dépôt, aucun test ni aucun point d’entrée. Il faut d’abord déterminer s’il s’agit d’une modification de la documentation du site web React ou d’une question d’utilisation propre à une application ; la tâche est considérée comme terminée lorsqu’un comportement clairement délimité et reproductible ainsi qu’une cible de documentation ou de code convenue sont établis.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript, react
- Domaine
- frontend, web-dev
- Type d'issue
- Bug
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- À clarifier
- Accessibilité débutants
- 15/100