[英]React app crashes when re-render
I have multiple routes in my react, redux application and when navigating to a new route everything works fine, but when I refresh the page it crashes, because it tries you use data that haven't been fetched yet: This is inside of my reducer) 我的react,redux应用程序中有多个路由,导航到新路由时一切正常,但是刷新页面时会崩溃,因为它会尝试使用尚未获取的数据:这在我的reducer内部)
export const getDoorById = (reduxStore, door) => {
console.log(reduxStore.fetchDoors.doors) // Nothing here
return reduxStore.fetchDoors.doors.find(item => item._id == door)
}
So I tried to make a "LoadingWrapper" around all of my components: 因此,我尝试围绕所有组件制作一个“ LoadingWrapper”:
export default class LoadingWrapper extends Component {
render() {
if (this.props.isLoading) {
return <CircularProgress />
} else {
return this.props.children
}
}
}
And it works fine on components who actually dispatches isLoading = true, like here: 它在实际调度isLoading = true的组件上运行良好,如下所示:
export function fetchEvents(state = initialState, action) {
switch (action.type) {
// CUSTOMERS
case 'FETCH_CUSTOMERS':
return { ...state, isLoading: true }
But on some of the other components reducers (they are exact same) it doesnt work and no loading icon shows up. 但是在其他一些减速器上(它们完全相同),它不起作用,并且没有显示加载图标。 So everything gets messed up.
所以一切都搞砸了。
I just wonder how I can fix this annoying bug that makes me unable to refresh the page. 我只是想知道如何解决这个烦人的bug,使我无法刷新页面。
Here's my reducer: 这是我的减速器:
const initialState = {
isLoading: false
}
export function fetchDoors(state = initialState, action) {
switch (action.type) {
case 'FETCH_DOORS':
return { ...state, isLoading: true }
case 'FETCH_DOORS_SUCCESS':
return { ...state, doors: action.payload.setDoors, isLoading: false }
case 'FETCH_CONTROLLERS':
return { ...state, isLoading: true }
case 'FETCH_CONTROLLERS_SUCCESS':
return {
...state,
controllers: action.payload.setControllers,
isLoading: false
}
default:
return state
}
}
// This tries to get the array of 'doors'
export const getDoorById = (reduxStore, door) => {
return reduxStore.fetchDoors.doors.find(item => item._id == door)
}
export const getControllerById = (reduxStore, controllerId) => {
return reduxStore.fetchDoors.controllers.find(
item => item._id == controllerId
)
}
render() {
if(!this.props.children && !isLoading) return null;
if (this.props.isLoading) {
return <CircularProgress />
} else {
return this.props.children
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.