我在 Relay Modern 中设置 refetchContainer 时遇到了一些问题。父组件是 QueryRenderer,它运行初始查询,适当地填充子组件的 props (a-prop-riately?eh?eh?!)。refetchContainer 指定了我们所有的变量,并且在输入字段的 onChange 事件中,使用新变量重新运行查询。这一切都很完美,除了孩子的道具永远不会随着收到的新数据而更新。我可以深入了解 Relay 存储,并查看确实收到了带有适当数据的查询。一段时间以来一直在努力解决这个问题,我将不胜感激。可能我缺少一些简单的东西。上帝知道 Relay Modern 的文档很少。
我四处寻找,找不到合适的解决方案。这家伙似乎遇到了类似的问题: relay refetch does not show the result
带有 QueryRenderer 的父组件:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { graphql, QueryRenderer } from 'react-relay';
import Search from './Search';
const propTypes = {
auth: PropTypes.object.isRequired,
};
class SearchContainer extends Component {
render() {
return (
<QueryRenderer
query={graphql`
query SearchContainerQuery($search: String!){
users: searchUsers(search:$search, first:10){
...Search_users
}
}`}
variables={{ search: 'someDefaultSearch' }}
environment={this.props.auth.environment}
render={({ error, props }) => {
if (error) {
console.log(error);
}
if (props) {
return <Search users={props.users} />;
}
return <div>No Dice</div>;
}}
/>
);
}
}
SearchContainer.propTypes = propTypes;
export default connect(state => ({ auth: state.auth }))(SearchContainer);
带有 createRefetchContainer 的子组件:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { createRefetchContainer, graphql } from 'react-relay';
const propTypes = {
relay: PropTypes.object.isRequired,
users: PropTypes.object,
};
const defaultProps = {
users: {},
};
class Search extends Component {
render() {
return (
<div>
<input
type="text"
onChange={(e) => {
e.preventDefault();
this.props.relay.refetch({
search: e.target.value,
});
}}
/>
<ul>
{this.props.users.nodes.map(user =>
<li key={user.id}>{user.username}</li>,
)}
</ul>
</div>
);
}
}
Search.propTypes = propTypes;
Search.defaultProps = defaultProps;
export default createRefetchContainer(
Search,
{
users: graphql.experimental`
fragment Search_users on SearchUsersConnection
@argumentDefinitions(
search: {type: "String", defaultValue: ""}
) {
nodes {
id
displayName
username
}
}
`,
},
graphql.experimental`
query SearchRefetchQuery($search: String!) {
users: searchUsers(search:$search, first:10){
...Search_users @arguments(search: $search)
}
}
`,
);
GraphQL 看起来像这样:
# A connection to a list of `User` values.
type SearchUsersConnection {
# Information to aid in pagination.
pageInfo: PageInfo!
# The count of *all* `User` you could get from the connection.
totalCount: Int
# A list of edges which contains the `User` and cursor to aid in pagination.
edges: [SearchUsersEdge]
# A list of `User` objects.
nodes: [User]
}
适当地进行网络调用,并按预期返回数据。 网络通话
似乎 @arguments 指令可以被排除在 refetch 查询之外:
query SearchRefetchQuery($search: String!) {
users: searchUsers(search:$search, first:10){
...Search_users @arguments(search: $search)
}
}
(删除它似乎没有效果)
我已经尝试按照此处的建议将@arguments 指令添加到父组件的片段中:将变量传递到中继现代中的片段容器,但没有效果。