我从中继开始,并试图使我的路由工作正常。不幸的是,我运气不太好。
下面是我遇到的错误:
Uncaught : Component.getFragment不是函数
下面是我的代码,供您参考:
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建议的修改,如下所示。但这提出了以下两个问题:
Home以外的组件由App组件呈现时,会发生什么情况?警告: RelayContainer:预期
store将提供给Home,但得到undefined。如果这是有意的,则传递一个显式null。
以下是一些变化:
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')}
}
`
}
});发布于 2016-04-18 11:38:34
片段定义实际上并不将组件作为参数,而是容器的变量映射,您只需要使用它们就可以根据变量值拥有条件片段。
中继路由查询不接受任何参数。
以下是您需要做的更改。
中继中的路由查询只需要指定要在此路由中检索的根查询,而不需要片段。
index.jsx
const AppQueries = {
store: () => Relay.QL `query {
store
}`
};您的实际上不使用任何中继数据,因此您可以只导出普通组件而不是容器。
export default class App extends Component {如果需要向其传递一些中继数据,则不需要包含子片段,因为只有在将其他容器直接呈现为直接子容器(而不是作为this.props.children)时,才需要包含片段。
然后,在路由器中,您需要将查询移到Home。
ReactDOM.render(
<RelayRouter history={browserHistory}>
<Route path='/' component={App}>
<IndexRoute component={Home} queries={AppQueries} />
</Route>
</RelayRouter>,
document.getElementById('ch-root'));https://stackoverflow.com/questions/36687203
复制相似问题