Skip to content
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

Merged
merged 9 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/packages/**/node_modules
/packages/**/dist
/lerna-debug.log
.history/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要加这个。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我先删掉。但实际上最好有这个说明,不然vscode有些装了这个插件的,会默认生成

4 changes: 2 additions & 2 deletions docs/guide/source-code-explore.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ function getProvider(store, app, router) {

// 真正的 react 在这里
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)))
}
```

Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -42,5 +42,6 @@
"prettier --trailing-comma all --single-quote --write",
"git add"
]
}
}
},
"repository": "[email protected]:jasonzhuang/dva18.git"
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1、去掉 repository
2、结尾得有空行

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

6 changes: 3 additions & 3 deletions packages/dva/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dva",
"version": "2.6.0-beta.23",
"version": "3.0.0",
"description": "React and redux based, lightweight and elm-style framework.",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down Expand Up @@ -45,8 +45,8 @@
"redux": "^4.0.1"
},
"peerDependencies": {
"react": "^16.8.4",
"react-dom": "^16.8.4"
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"files": [
"dist",
Expand Down
125 changes: 61 additions & 64 deletions packages/dva/src/index.js
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,
Expand All @@ -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 = {}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要做风格调整,应该是你本地的 prettier 和 dva 的 prettier 版本不一致。

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand All @@ -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 }