-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support react 18 #2515
feat: support react 18 #2515
Changes from 5 commits
40c4568
9889184
56f36f9
b5a3df2
1b040e3
1e85067
d220659
549d708
cee13e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
/packages/**/node_modules | ||
/packages/**/dist | ||
/lerna-debug.log | ||
.history/ | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,8 +31,8 @@ | |
"lerna-changelog": "^0.8.0", | ||
"lint-staged": "^7.2.2", | ||
"prettier": "^1.14.3", | ||
"react": "^16.8.4", | ||
"react-dom": "^16.8.4", | ||
"react": "^18.0.0", | ||
"react-dom": "^18.0.0", | ||
"react-testing-library": "^6.0.0", | ||
"shelljs": "^0.8.1", | ||
"umi-test": "^1.5.2" | ||
|
@@ -42,5 +42,6 @@ | |
"prettier --trailing-comma all --single-quote --write", | ||
"git add" | ||
] | ||
} | ||
} | ||
}, | ||
"repository": "[email protected]:jasonzhuang/dva18.git" | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1、去掉 repository There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import React from 'react'; | ||
import invariant from 'invariant'; | ||
import { createBrowserHistory, createMemoryHistory, createHashHistory } from 'history'; | ||
import document from 'global/document'; | ||
import React from 'react' | ||
import invariant from 'invariant' | ||
import { createBrowserHistory, createMemoryHistory, createHashHistory } from 'history' | ||
import document from 'global/document' | ||
import { | ||
Provider, | ||
connect, | ||
|
@@ -10,101 +10,98 @@ import { | |
useDispatch, | ||
useStore, | ||
shallowEqual, | ||
} from 'react-redux'; | ||
import { bindActionCreators } from 'redux'; | ||
import { utils, create, saga } from 'dva-core'; | ||
import * as router from 'react-router-dom'; | ||
import * as routerRedux from 'connected-react-router'; | ||
|
||
const { connectRouter, routerMiddleware } = routerRedux; | ||
const { isFunction } = utils; | ||
const { useHistory, useLocation, useParams, useRouteMatch } = router; | ||
|
||
export default function(opts = {}) { | ||
const history = opts.history || createHashHistory(); | ||
} from 'react-redux' | ||
import { bindActionCreators } from 'redux' | ||
import { utils, create, saga } from 'dva-core' | ||
import * as router from 'react-router-dom' | ||
import * as routerRedux from 'connected-react-router' | ||
|
||
const { connectRouter, routerMiddleware } = routerRedux | ||
const { isFunction } = utils | ||
const { useHistory, useLocation, useParams, useRouteMatch } = router | ||
|
||
export default function (opts = {}) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不要做风格调整,应该是你本地的 prettier 和 dva 的 prettier 版本不一致。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, 暂时把vscode上formatOnSave关闭了解决 |
||
const history = opts.history || createHashHistory() | ||
const createOpts = { | ||
initialReducer: { | ||
router: connectRouter(history), | ||
}, | ||
setupMiddlewares(middlewares) { | ||
return [routerMiddleware(history), ...middlewares]; | ||
return [routerMiddleware(history), ...middlewares] | ||
}, | ||
setupApp(app) { | ||
app._history = patchHistory(history); | ||
app._history = patchHistory(history) | ||
}, | ||
}; | ||
} | ||
|
||
const app = create(opts, createOpts); | ||
const oldAppStart = app.start; | ||
app.router = router; | ||
app.start = start; | ||
return app; | ||
const app = create(opts, createOpts) | ||
const oldAppStart = app.start | ||
app.router = router | ||
app.start = start | ||
return app | ||
|
||
function router(router) { | ||
invariant( | ||
isFunction(router), | ||
`[app.router] router should be function, but got ${typeof router}`, | ||
); | ||
app._router = router; | ||
) | ||
app._router = router | ||
} | ||
|
||
function start(container) { | ||
// 允许 container 是字符串,然后用 querySelector 找元素 | ||
if (isString(container)) { | ||
container = document.querySelector(container); | ||
invariant(container, `[app.start] container ${container} not found`); | ||
container = document.querySelector(container) | ||
invariant(container, `[app.start] container ${container} not found`) | ||
} | ||
|
||
// 并且是 HTMLElement | ||
invariant( | ||
!container || isHTMLElement(container), | ||
`[app.start] container should be HTMLElement`, | ||
); | ||
invariant(!container || isHTMLElement(container), `[app.start] container should be HTMLElement`) | ||
|
||
// 路由必须提前注册 | ||
invariant(app._router, `[app.start] router must be registered before app.start()`); | ||
invariant(app._router, `[app.start] router must be registered before app.start()`) | ||
|
||
if (!app._store) { | ||
oldAppStart.call(app); | ||
oldAppStart.call(app) | ||
} | ||
const store = app._store; | ||
const store = app._store | ||
|
||
// export _getProvider for HMR | ||
// ref: https://github.com/dvajs/dva/issues/469 | ||
app._getProvider = getProvider.bind(null, store, app); | ||
app._getProvider = getProvider.bind(null, store, app) | ||
|
||
// If has container, render; else, return react component | ||
if (container) { | ||
render(container, store, app, app._router); | ||
app._plugin.apply('onHmr')(render.bind(null, container, store, app)); | ||
render(container, store, app, app._router) | ||
app._plugin.apply('onHmr')(render.bind(null, container, store, app)) | ||
} else { | ||
return getProvider(store, this, this._router); | ||
return getProvider(store, this, this._router) | ||
} | ||
} | ||
} | ||
|
||
function isHTMLElement(node) { | ||
return typeof node === 'object' && node !== null && node.nodeType && node.nodeName; | ||
return typeof node === 'object' && node !== null && node.nodeType && node.nodeName | ||
} | ||
|
||
function isString(str) { | ||
return typeof str === 'string'; | ||
return typeof str === 'string' | ||
} | ||
|
||
function getProvider(store, app, router) { | ||
const DvaRoot = extraProps => ( | ||
<Provider store={store}>{router({ app, history: app._history, ...extraProps })}</Provider> | ||
); | ||
return DvaRoot; | ||
) | ||
return DvaRoot | ||
} | ||
|
||
function render(container, store, app, router) { | ||
const ReactDOM = require('react-dom'); // eslint-disable-line | ||
ReactDOM.render(React.createElement(getProvider(store, app, router)), container); | ||
const ReactDOM = require('react-dom/client') // eslint-disable-line | ||
ReactDOM.createRoot(container).render(React.createElement(getProvider(store, app, router))) | ||
} | ||
|
||
function patchHistory(history) { | ||
const oldListen = history.listen; | ||
const oldListen = history.listen | ||
history.listen = callback => { | ||
// TODO: refact this with modified ConnectedRouter | ||
// Let ConnectedRouter to sync history to store first | ||
|
@@ -116,35 +113,35 @@ function patchHistory(history) { | |
// r.inTimeTravelling ? r.inTimeTravelling = !1 : a(e, t, n) | ||
// } | ||
// ref: https://github.com/umijs/umi/issues/2693 | ||
const cbStr = callback.toString(); | ||
const cbStr = callback.toString() | ||
const isConnectedRouterHandler = | ||
(callback.name === 'handleLocationChange' && cbStr.indexOf('onLocationChanged') > -1) || | ||
(cbStr.indexOf('.inTimeTravelling') > -1 && | ||
cbStr.indexOf('.inTimeTravelling') > -1 && | ||
cbStr.indexOf('arguments[2]') > -1); | ||
cbStr.indexOf('arguments[2]') > -1) | ||
// why add __isDvaPatch: true | ||
// since it's a patch from dva, we need to identify it in the listen handlers | ||
callback(history.location, history.action, { __isDvaPatch: true }); | ||
callback(history.location, history.action, { __isDvaPatch: true }) | ||
return oldListen.call(history, (...args) => { | ||
if (isConnectedRouterHandler) { | ||
callback(...args); | ||
callback(...args) | ||
} else { | ||
// Delay all listeners besides ConnectedRouter | ||
setTimeout(() => { | ||
callback(...args); | ||
}); | ||
callback(...args) | ||
}) | ||
} | ||
}); | ||
}; | ||
return history; | ||
}) | ||
} | ||
return history | ||
} | ||
|
||
export fetch from 'isomorphic-fetch'; | ||
export dynamic from './dynamic'; | ||
export { connect, connectAdvanced, useSelector, useDispatch, useStore, shallowEqual }; | ||
export { bindActionCreators }; | ||
export { router }; | ||
export { saga }; | ||
export { routerRedux }; | ||
export { createBrowserHistory, createMemoryHistory, createHashHistory }; | ||
export { useHistory, useLocation, useParams, useRouteMatch }; | ||
export fetch from 'isomorphic-fetch' | ||
export dynamic from './dynamic' | ||
export { connect, connectAdvanced, useSelector, useDispatch, useStore, shallowEqual } | ||
export { bindActionCreators } | ||
export { router } | ||
export { saga } | ||
export { routerRedux } | ||
export { createBrowserHistory, createMemoryHistory, createHashHistory } | ||
export { useHistory, useLocation, useParams, useRouteMatch } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不要加这个。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我先删掉。但实际上最好有这个说明,不然vscode有些装了这个插件的,会默认生成