gpt4 book ai didi

reactjs - 如何让Relay和React Routing正常工作?

转载 作者:行者123 更新时间:2023-12-03 13:44:26 24 4
gpt4 key购买 nike

我开始使用中继并尝试使我的路由正常工作。不幸的是,我的运气不太好。

这是我收到的错误:

Uncaught TypeError: Component.getFragment is not a function

这是我提供的代码供您引用:

index.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import Relay from 'react-relay';
import {Router, Route, IndexRoute, browserHistory} from 'react-router';
import {RelayRouter} from 'react-router-relay';

import App from './modules/app';
import Home from './modules/home';

const AppQueries = {
store: (Component) => Relay.QL `query {
store {
${Component.getFragment('store')}
}
}`
};

ReactDOM.render(
<RelayRouter history={browserHistory}>
<Route path='/' component={App} queries={AppQueries}>
<IndexRoute component={Home}/>
</Route>
</RelayRouter>,
document.getElementById('ch-root'));

app.jsx

import React, {Component} from 'react';
import Relay from 'react-relay';
import Header from './ui/header';
import Footer from './ui/footer';

class App extends Component {
render() {
return (
<div id="ch-container">
<Header/>
<section id="ch-body">
{this.props.children}
</section>
<Footer/>
</div>
);
}
}

App = Relay.createContainer(App, {
fragments: {
store: (Component) => Relay.QL `
fragment on Store {
${Component.getFragment('store')}
}
`
}
});

export default App;

home.jsx

import React, {Component} from 'react';
import Relay from 'react-relay';
import Loader from './ui/loader';
import AccountSelector from './account/account-selector';

const APP_HOST = window.CH_APP_HOST;
const CURR_HOST = `${window.location.protocol}//${window.location.host}`;

class Home extends Component {
state = {
activeAccount: null,
loading: true
}

render() {
const {activeAccount, loading} = this.state;

if (loading) {
return <Loader/>;
}

if (!activeAccount && !loading) {
return <AccountSelector/>;
}

return (
<h1>Hello!</h1>
);
}
}

Home = Relay.createContainer(Home, {
fragments: {
store: () => Relay.QL `
fragment on Store {
accounts {
unique_id,
subdomain
}
}
`
}
});

export default Home;

更新

我按照 freiksenet 的建议进行了一些更改,如下所示。但这引发了以下两个问题:

  1. 当我更改路线并且 Home 以外的组件由 App 组件渲染时会发生什么?
  2. 我现在收到此错误:

Warning: RelayContainer: Expected prop store to be supplied to Home, but got undefined. Pass an explicit null if this is intentional.

以下是更改:

index.jsx

const AppQueries = {
store: () => Relay.QL `query {
store
}`
};

app.jsx

import Home from "./home";

...

App = Relay.createContainer(App, {
fragments: {
store: () => Relay.QL `
fragment on Store {
${Home.getFragment('store')}
}
`
}
});

最佳答案

片段定义实际上并不获取组件作为参数,而是容器的变量映射,您只需要使用它们就可以根据变量值获得条件片段。

中继路由查询不接受任何参数。

以下是您需要进行的更改。

Relay 中的路由查询只需指定要在此路由中检索的根查询,无需片段。

index.jsx

const AppQueries = {
store: () => Relay.QL `query {
store
}`
};

您的应用程序组件实际上不使用任何中继数据,因此您可以只导出普通组件而不是容器。

export default class App extends Component {

如果您需要向其传递一些中继数据,则不需要包含子片段,因为仅当您直接将其他容器渲染为直接子容器(而不是 this.props.children)时才需要包含片段.

然后在路由器中,您需要将查询移至主页。

ReactDOM.render(
<RelayRouter history={browserHistory}>
<Route path='/' component={App}>
<IndexRoute component={Home} queries={AppQueries} />
</Route>
</RelayRouter>,
document.getElementById('ch-root'));

关于reactjs - 如何让Relay和React Routing正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36687203/

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