Browse Source

Fix setState on unmounted component, close #338

afc163 8 năm trước cách đây
mục cha
commit
1e315ca34a

+ 12 - 6
src/models/chart.js

@@ -14,10 +14,15 @@ export default {
     salesTypeDataOnline: [],
     salesTypeDataOnline: [],
     salesTypeDataOffline: [],
     salesTypeDataOffline: [],
     radarData: [],
     radarData: [],
+    loading: false,
   },
   },
 
 
   effects: {
   effects: {
     *fetch(_, { call, put }) {
     *fetch(_, { call, put }) {
+      yield put({
+        type: 'changeLoading',
+        payload: true,
+      });
       const response = yield call(fakeChartData);
       const response = yield call(fakeChartData);
       yield put({
       yield put({
         type: 'save',
         type: 'save',
@@ -40,12 +45,7 @@ export default {
       return {
       return {
         ...state,
         ...state,
         ...payload,
         ...payload,
-      };
-    },
-    setter(state, { payload }) {
-      return {
-        ...state,
-        ...payload,
+        loading: false,
       };
       };
     },
     },
     clear() {
     clear() {
@@ -62,5 +62,11 @@ export default {
         radarData: [],
         radarData: [],
       };
       };
     },
     },
+    changeLoading(state, { payload }) {
+      return {
+        ...state,
+        loading: payload,
+      };
+    },
   },
   },
 };
 };

+ 3 - 3
src/routes/Dashboard/Analysis.js

@@ -27,7 +27,6 @@ for (let i = 0; i < 7; i += 1) {
 }))
 }))
 export default class Analysis extends Component {
 export default class Analysis extends Component {
   state = {
   state = {
-    loading: true,
     salesType: 'all',
     salesType: 'all',
     currentTabKey: '',
     currentTabKey: '',
     rangePickerValue: [],
     rangePickerValue: [],
@@ -36,7 +35,7 @@ export default class Analysis extends Component {
   componentDidMount() {
   componentDidMount() {
     this.props.dispatch({
     this.props.dispatch({
       type: 'chart/fetch',
       type: 'chart/fetch',
-    }).then(() => this.setState({ loading: false }));
+    });
   }
   }
 
 
   componentWillUnmount() {
   componentWillUnmount() {
@@ -90,7 +89,7 @@ export default class Analysis extends Component {
   }
   }
 
 
   render() {
   render() {
-    const { rangePickerValue, salesType, currentTabKey, loading } = this.state;
+    const { rangePickerValue, salesType, currentTabKey } = this.state;
     const { chart } = this.props;
     const { chart } = this.props;
     const {
     const {
       visitData,
       visitData,
@@ -102,6 +101,7 @@ export default class Analysis extends Component {
       salesTypeData,
       salesTypeData,
       salesTypeDataOnline,
       salesTypeDataOnline,
       salesTypeDataOffline,
       salesTypeDataOffline,
+      loading,
     } = chart;
     } = chart;
 
 
     const salesPieData = salesType === 'all' ?
     const salesPieData = salesType === 'all' ?