我有这样的路线
<Route path="/search" component={Search}>
基本Search
组件看起来像这样
class Search extends React.Component {
constructor (props) {
super(props)
this.state = {query: ''}
}
handleSubmit (event) {
event.preventDefault()
this.setState({query: this.refs.queryInput.value})
}
renderSearchResult() {
if (this.state.query === '')
return <EmptySearchResult />
else
return <SearchResult query={this.state.query}/>
}
render() {
return (
<div className="searchContainer">
<div className="row">
<div className="search">
<form onSubmit={event => this.handleSubmit(event)}>
<input className="searchInput" placeholder="robocop" ref="queryInput" />
</form>
</div>
</div>
<div className="row">
{this.renderSearchResult()}
</div>
</div>
)
}
}
SearchResult
中继容器看起来像这样
class SearchResult extends React.Component {
render() {
var {viewer: {moviesByTitle: movies}} = this.props;
return (
<div className="searchResult">
{movies.edges.map((edge,i) =>
<div key={i} className="rowItem scrollRowItem">
<Movie movie={edge.node} />
</div>)}
</div>
)
}
}
export default Relay.createContainer(SearchResult, {
initialVariables: {
query: '???'
},
fragments: {
viewer: () => Relay.QL`
fragment on User {
moviesByTitle(title: $query, first: 10) {
edges {
node {
${Movie.getFragment('movie')}
}
}
}
}
`
}
})
错误:
Warning: RelayContainer: Expected prop `viewer` to be supplied to `SearchResult`, but got `undefined`. Pass an explicit `null` if this is intentional.
我在搜索组件中尝试做的事情(更改为粗体)
const ViewerQueries = {
viewer: () => Relay.QL`query { viewer }`
}
...
renderSearchResult() {
if (this.state.query === '')
return <EmptySearchResult />
else
return <SearchResult query={this.state.query} queries={ViewerQueries} />
}
但当然这不起作用,因为查询需要以某种方式附加到Route
问题
我的
Search
组件只是一个展示组件,不需要自己的数据。相反,它只是向中继容器提供一个query
道具。SearchResult
我应该如何构建组件和中继容器以正确地将它们附加到路由?如何使用
SearchResult
query
道具在中继查询片段中设置变量?我一般不理解“查看器”的抽象——我见过的大多数例子都非常做作,并没有显示它们在更现实的环境中是如何工作的;例如,对不同资源具有不同访问权限的用户,或旨在查看不同数据片段的程序的不同部分