简体   繁体   English

ReactJS / GraphQL:通过提交按钮调用查询

[英]ReactJS/GraphQL: Call query from submit button

When my app loads the query below runs and the result from the DB is displayed in the browser. 当我的应用程序加载时,将运行以下查询,并且来自数据库的结果将显示在浏览器中。 However, my app also has a submit button. 但是,我的应用程序也有一个提交按钮。 How can run this entire component when the submit button is pressed while passing the timestamp input from the submit? 传递提交的时间戳输入时,如果按下提交按钮,如何运行整个组件?

handleSubmit(event) {
  event.preventDefault();
  console.log(this.state.inputValue)
  this.state = {
    inputValue: new Date(document.getElementById("time").value).valueOf()
  };
  console.log(this.state);            
}

This is the UserList component code: 这是UserList组件代码:

const UserList = props => (
  <Query
    query={gql`
      query action($timestamp: Float!) {
        action(timestamp: $timestamp) {
          action
          timestamp
          object {
            filename
          }
        }
      }
    `}
  >
    {({ loading, error, data }) => {
      if (loading) return <p>Loading...</p>;
      if (error) return <p>Error</p>;

      return (
        <Item.Group divided>
          {data.action.map(action => (
            <div>
              <ul>
                <li>{action.action}</li>
                <li>{action.timestamp}</li>
                <ul>
                  {action.object.map(obj => {
                    return <li>{obj.filename}</li>;
                  })}
                </ul>
              </ul>
            </div>
          ))}
        </Item.Group>
      );
    }}
  </Query>
);

export default UserList;

Parent component contains submit button and there lives state. 父组件包含“提交”按钮,并且存在生存状态。 Submit handler should only set value in state with ... setState() - NOT directly! 提交处理程序仅应使用... setState()设置状态值-不能直接设置!

If there is no default state/value use conditional rendering ... in parent render display some placeholder when state (timestamp) is undefined. 如果没有默认状态/值,则在未定义状态(时间戳)时,在父渲染中使用条件渲染...会显示一些占位符。 If value exists pass it as prop: 如果值存在,则将其作为prop传递:

{!this.state.inputValue ? <SomePlaceholder />
: <UserList timestamp={this.state.inputValue} />}
// place input, submit button where you want (after/before results)

UserList will be called with props - 'props.timestamp' can be used in graphql query literal. UserList将通过props调用-'props.timestamp'可以在graphql查询文字中使用。

When input changes again submit handler will change parent state and that change will be passed again as prop to child, query fired, results rerendered. 当输入再次更改时,提交处理程序将更改父状态,并且该更改将再次作为prop传递给子对象,触发查询,结果重新呈现。

Base on the information provided, there's probably several ways to accomplish what you need. 根据提供的信息,可能有几种方法可以满足您的需求。 I would say the UserList component needs to conditionally render the Query component in some manner. 我想说UserList组件需要以某种方式有条件地呈现Query组件。 Perhaps UserList takes a boolean prop isSubmitted and while isSubmitted is false , UserList renders a div with text explaining to submit the query. 也许UserList接受一个布尔值isSubmitted ,而isSubmittedfalse ,则UserList呈现一个div并带有解释提交查询的文本。 This could be done as the UserList and the submit button being hosted by the same parent component that has state containing a isSubmitted property that changes on click. 可以这样做,因为UserListisSubmitted按钮由同一个父组件承载,该父组件的state包含单击时更改的isSubmitted属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM