Save previous view when move data fetch from componentWillUpdate(WillReceiveProps) to ComponentDidUpdate
還沒有人認領這個 Issue。
- 主要語言
- JavaScript
- 星號
- 11.8k
- 分支
- 7.9k
- 平均合併
- 1 天 11 小時
- 30 天內合併 PR
- 11
描述
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
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
該 issue 提供了使用 componentWillReceiveProps、componentDidUpdate、shouldComponentUpdate 和 fetchRoutes 的生命週期範例,但沒有指出任何 repository 檔案、測試或進入點。首先確定這是 React 網站的文件修改,還是特定於應用程式的使用問題;只有明確定義出範圍清楚且可重現的行為,以及經過同意的文件或程式碼目標,才算完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- javascript, react
- 領域
- frontend, web-dev
- Issue 類型
- 缺陷
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 需要釐清
- 新手友好度
- 15/100