-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
67 lines (60 loc) · 1.68 KB
/
Copy pathindex.js
File metadata and controls
67 lines (60 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React from 'react';
import { createRoot } from 'react-dom/client';
import { StrictMode } from 'react';
import dva, { react18Utils } from 'dva';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import App from './App';
// 1. Initialize dva app
const app = dva();
// 2. Define models with proper cleanup
app.model({
namespace: 'counter',
state: { count: 0 },
reducers: {
increment(state) {
return { ...state, count: state.count + 1 };
},
decrement(state) {
return { ...state, count: state.count - 1 };
}
},
effects: {
*asyncIncrement(action, { put, call }) {
// Simulate async operation
yield call(() => new Promise(resolve => setTimeout(resolve, 1000)));
yield put({ type: 'increment' });
}
},
subscriptions: {
setup({ dispatch, history }) {
// Proper cleanup function
const handleVisibilityChange = () => {
if (document.hidden) {
console.log('Page is hidden - pausing operations');
} else {
console.log('Page is visible - resuming operations');
}
};
document.addEventListener('visibilitychange', handleVisibilityChange);
// Return cleanup function for Strict Mode
return () => {
document.removeEventListener('visibilitychange', handleVisibilityChange);
};
}
}
});
// 3. Start app
app.start();
// 4. Create React 18 root with Strict Mode
const container = document.getElementById('root');
const root = createRoot(container);
root.render(
<StrictMode>
<Provider store={app._store}>
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>
</StrictMode>
);