gpt4 book ai didi

react-router - 将 react-router 和 react-router-relay 从 v1.x 更新到 v2.x(位置 "/"不匹配任何路由)

转载 作者:行者123 更新时间:2023-12-02 01:23:20 42 4
gpt4 key购买 nike

我正在尝试在我的应用程序中将 react-router 更新到 v2.6 并将 react-router-relay 更新到 v0.7,但我正在努力按照变更日志来解决所有重大变更。我想我解决了所有的变化,但我仍然无法让它发挥作用。

Warning: [react-router] Location "/" did not match any routes

这是我为解决这些变化所做的分步指南。首先我更新了 npm 模块。

我之前的 package.json 一切正常:

"dependencies": {
"babel-polyfill": "^6.9.1",
"babel-runtime": "^6.9.2",
"graphiql": "0.7.3",
"graphql": "^0.6.2",
"history": "1.13.1",
"isomorphic-fetch": "^2.1.1",
"react": "^15.2.1",
"react-addons-shallow-compare": "^15.2.1",
"react-dom": "^15.2.1",
"react-loader": "^2.0.0",
"react-relay": "^0.9.2",
"react-router": "1.0.0-rc3",
"react-router-relay": "^0.7.0",
"superagent": "^1.2.0"
},
"devDependencies": {
"babel-core": "^6.11.4",
"babel-eslint": "^6.1.2",
"babel-jest": "^13.2.2",
"babel-loader": "^6.2.4",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-transform-runtime": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.11.1",
"babel-preset-stage-0": "^6.5.0",
"babel-preset-stage-1": "^6.5.0",
"babel-preset-stage-2": "^6.11.0",
"babel-relay-plugin": "^0.9.2",
"jest-cli": "^12.1.1",
"react-addons-test-utils": "^15.2.1",
"webpack": "^1.13.1"
},

我的新 package.json,我在其中遇到错误:

"dependencies": {
"babel-polyfill": "^6.9.1",
"babel-runtime": "^6.9.2",
"graphiql": "0.7.3",
"graphql": "^0.6.2",
"isomorphic-fetch": "^2.1.1",
"react": "^15.2.1",
"react-addons-shallow-compare": "^15.2.1",
"react-dom": "^15.2.1",
"react-loader": "^2.0.0",
"react-relay": "^0.9.2",
"react-router": "^2.6.0",
"react-router-relay": "^0.13.3",
"superagent": "^1.2.0"
},
"devDependencies": {
"babel-core": "^6.11.4",
"babel-eslint": "^6.1.2",
"babel-jest": "^14.0.0",
"babel-loader": "^6.2.4",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-transform-runtime": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.11.1",
"babel-preset-stage-0": "^6.5.0",
"babel-preset-stage-1": "^6.5.0",
"babel-preset-stage-2": "^6.11.0",
"babel-relay-plugin": "^0.9.2",
"jest-cli": "^12.1.1",
"react-addons-test-utils": "^15.2.1",
"webpack": "^1.13.1"
},

我删除了 history 模块,因为它现在是 react-router 的直接依赖项。安装最新的模块后,我重新加载了我的页面并收到以下错误:

错误 1:

Warning: [react-router] It appears you have provided a deprecated history object to <Router/>, please use a history provided by React Router with import { browserHistory } from 'react-router' or import { hashHistory } from 'react-router'. If you are using a custom history please create it with useRouterHistory, see https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#using-history-with-router for details.

错误2:

Warning: Failed context type: Invalid prop/context relay supplied to Relay(App), expected undefined to be an object conforming to the RelayEnvironment interface.

in Relay(App) (created by RouterContext)
in RouterContext (created by Router)
in Router

错误 3:

Warning: Failed context type: Required context route was not specified in Relay(App).

in Relay(App) (created by RouterContext)
in RouterContext (created by Router)
in Router

错误4:

Uncaught Invariant Violation: RelayContainer: Relay(App) was rendered with invalid Relay context undefined. Make sure the relay property on the React context conforms to the RelayEnvironment interface.

第一个错误提供了指向 react-router 升级指南的便捷链接,因此我遵循它并相应地更新了我的 app.js 文件。

在我改变之前它看起来是这样的:

import babelPolyfill from 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import ReactRouterRelay from 'react-router-relay';
import Loader from 'react-loader';
import { Router, Route } from 'react-router';
import createBrowserHistory from 'history/lib/createBrowserHistory';

import WelcomeQueries from './queries/WelcomeQueries';
import AppQueries from './queries/AppQueries';
import LandingQueries from './queries/LandingQueries';

import App from './components/app';
import Landing from './components/landing';

import './relay';

const renderProps = {
renderLoading: () => <Loader />,
};

ReactDOM.render((
<Router history={createBrowserHistory()} createElement={ReactRouterRelay.createElement}>
<Route component={App} queries={AppQueries} {...renderProps}>
<Route path="/myapp" component={Landing} queries={LandingQueries} {...renderProps} />
</Route>
<Route path="/myapp/tracks" component={Welcome} queries={WelcomeQueries} {...renderProps}/>
</Router>
), document.getElementById('myapp-body'));

尝试 1

使用 2.x 的更新指南,我更改了我的 app.js 文件以使用这样的自定义历史记录:

import babelPolyfill from 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import ReactRouterRelay from 'react-router-relay';
import Loader from 'react-loader';

import { Router, Route, useRouterHistory } from 'react-router'
import { createHashHistory } from 'history'

import WelcomeQueries from './queries/WelcomeQueries';
import AppQueries from './queries/AppQueries';
import LandingQueries from './queries/LandingQueries';

import App from './components/app';
import Landing from './components/landing';

import './relay';

const renderProps = {
renderLoading: () => <Loader />,
};

const appHistory = useRouterHistory(createHashHistory)({ queryKey: false })

ReactDOM.render((
<Router history={appHistory} createElement={ReactRouterRelay.createElement}>
<Route component={App} queries={AppQueries} {...renderProps}>
<Route path="/myapp" component={Landing} queries={LandingQueries} {...renderProps} />
</Route>
<Route path="/myapp/tracks" component={Welcome} queries={WelcomeQueries} {...renderProps}/>
</Router>
), document.getElementById('myapp-body'));

然后在浏览器中出现以下错误:

错误 5

Warning: [react-router] Location "/" did not match any routes

我相信这是我想要的解决方案。但是我不确定,所以我也尝试了 Browser (HTML5 pushState) History 方式并尝试了以下更改:

尝试 2

import babelPolyfill from 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import ReactRouterRelay from 'react-router-relay';
import Loader from 'react-loader';

import { Router, Route, browserHistory } from 'react-router'

import WelcomeQueries from './queries/WelcomeQueries';
import AppQueries from './queries/AppQueries';
import LandingQueries from './queries/LandingQueries';

import App from './components/app';
import Landing from './components/landing';

import './relay';

const renderProps = {
renderLoading: () => <Loader />,
};

const appHistory = useRouterHistory(createHashHistory)({ queryKey: false })

ReactDOM.render((
<Router history={browserHistory} createElement={ReactRouterRelay.createElement}>
<Route component={App} queries={AppQueries} {...renderProps}>
<Route path="/myapp" component={Landing} queries={LandingQueries} {...renderProps} />
</Route>
<Route path="/myapp/tracks" component={Welcome} queries={WelcomeQueries} {...renderProps}/>
</Router>
), document.getElementById('myapp-body'));

加载它时,我返回错误 2、3 和 4。我认为尝试 1 是正确的方法,但我不知道这个错误是什么意思并用谷歌搜索它,给了我很多答案( 12 ,其中将我链接到 3,甚至使用 IndexRoute 链接到 4),我试过了,但没有一个答案有效。

我的 App.js 文件是这样的,仅供引用:

import React from 'react';
import Relay from 'react-relay';
import Top from './top';

export class App extends React.Component {
render() {
return (
<div>
<Top viewer={this.props.viewer}/>
{this.props.children}
</div>
);
}
}

export default Relay.createContainer(App, {
fragments: {
viewer: () => Relay.QL`
fragment on Viewer {
${Top.getFragment('viewer')}
}
`,
},
});

我不是 100% 确定这是否是原因,但我知道 react-router v2.0.0-rc6 发生了重大变化,顶级路由器导出被删除,但我真的不知道这对我意味着什么?那我不能再使用路由器了吗?如果是这样,我应该怎么做?

经过几天的研究和反复试验,我放弃了,希望这里有人能提供帮助。任何指向正确方向的指示都将不胜感激。

最佳答案

终于得到了这个问题的答案,不得不添加以下更改以使我的代码再次工作:

import babelPolyfill from 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import Loader from 'react-loader';

import Relay from 'react-relay';
import useRelay from 'react-router-relay';
import { Router, Route, IndexRoute, browserHistory } from 'react-router';
import applyRouterMiddleware from 'react-router/lib/applyRouterMiddleware';

import WelcomeQueries from './queries/WelcomeQueries';
import AppQueries from './queries/AppQueries';
import LandingQueries from './queries/LandingQueries';

import App from './components/app';
import Landing from './components/landing';

import './relay';

ReactDOM.render((
<Router
history={browserHistory}
render={applyRouterMiddleware(useRelay)}
environment={Relay.Store}>

<Route
component={App}
queries={AppQueries}
render={({ props }) => props ? <App {...props} /> : <Loader />}>

<Route
path="/myapp"
component={Landing}
queries={LandingQueries}
render={({ props }) => props ? <Landing {...props} /> : <Loader />} />
</Route>

<Route
path="/myapp/tracks"
component={Welcome}
queries={WelcomeQueries}
render={({ props }) => props ? <Welcome {...props} /> : <Loader />} />

</Router>
), document.getElementById('myapp-body'));

也许这会对其他人有所帮助:)

关于react-router - 将 react-router 和 react-router-relay 从 v1.x 更新到 v2.x(位置 "/"不匹配任何路由),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38645808/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com