gpt4 book ai didi

functional-programming - 如何将数据连接到 react-router-relay 中的深层组件?

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

我有这样的路线

<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.

我在 Search 组件中尝试执行的操作(粗体更改)
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 prop 在中继查询片段中设置一个变量?
  • 我不理解一般的“查看器”抽象——我见过的大多数例子都非常人为,没有展示它们在更现实的环境中是如何工作的;例如,对不同资源具有不同访问权限的用户,或旨在查看不同数据切片的程序的不同部分
  • 最佳答案

    使用 Relay 的现代 API,编写一个 refetchContainer来自 SearchResult组件,因为需要“一个选项来执行具有不同变量的新查询,并在请求返回时呈现该查询的响应”。

    import React, { Component } from 'react';
    import debounce from 'debounce';
    import { graphql, createRefetchContainer } from 'react-relay';

    class SearchResult extends React.Component {
    componentWillReceiveProps(nextProps) {
    if (this.props.query != nextProps.query) {
    debounce(() => this._searchMovies(), 600);
    }
    }

    _searchMovies() {
    const refetchVariables = fragmentVariables => ({
    query: this.props.query
    });

    this.props.relay.refetch(refetchVariables, null);
    }

    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>
    )
    }
    }


    // Similar query syntax as Relay Classic
    // Only syntactic difference is with absence of Movie.getFragment
    module.exports = createRefetchContainer(SearchResult, {
    viewer: graphql`
    fragment SearchResult_viewer on SearchResult {
    @argumentDefinitions(
    query: {type: "String", defaultValue: ""}
    ) {
    moviesByTitle(title: $query, first: 10) {
    edges {
    node {
    ...Movie_movie
    }
    }
    }
    }`
    },
    graphql`
    query MoviesRefetchQuery($query: String) {
    viewer {
    ...SearchResult_viewer @arguments(query: $query)
    }
    }
    `);

    关于functional-programming - 如何将数据连接到 react-router-relay 中的深层组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42652411/

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