如何使用useLazyQuery()对每次点击执行查询

时间:2019-10-07 06:27:12

标签: reactjs react-apollo apollo-client react-apollo-hooks

使用useLazyQuery()中的@apollo/react-hooks钩子,我可以通过单击按钮来执行查询。但是我不能使用它在连续点击时执行相同的查询。

export default ({ queryVariables }) => {
  const [sendQuery, { data, loading }] = useLazyQuery(GET_DIRECTION, {
    variables: queryVariables
  });

  return <div onClick={sendQuery}>Click here</div>;
}

上面的sendQuery仅执行一次。

2 个答案:

答案 0 :(得分:1)

useLazyQuery使用高速缓存优先的默认网络策略,所以我认为您的onClick函数实际上会执行,但是由于返回的值就是高速缓存中的值,因此React不会注意到数据的变化,因为自返回以来状态没有更新数据就是它已经拥有的那种方式,无需重新渲染,一切似乎都没有改变。我建议您应该传递其他网络策略,例如

 protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {    ///<summary> Change the mouse cursor to Hand symbol to show the user the cell is selectable</summary>
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';this.style.cursor='Pointer'";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

            ///<summary> Attach the click event to each cells</summary>
            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
        }
    }

    protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        // If multiple buttons are used in a GridView control, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "Select")
        {
            ///<summary>
            ///Convert the row index stored in the CommandArgument
            ///property to an Integer.
            ///</summary>
            LoadData(Convert.ToInt32(e.CommandArgument));

        }
    }

    private void LoadData(int? rowNumber = null)
    {
        //if rowNumber is null use GridView1.SelectedIndex
        var index = rowNumber ?? GridView1.SelectedIndex;

        // Retrieve the row that contains the button clicked 
        // by the user from the Rows collection.
        GridViewRow row = GridView1.Rows[index];

        //Populate the input box with the value of selected row.
        GridViewRow gr = GridView1.Rows[index];
        TextBox1.Text = gr.Cells[2].Text;
        TextBox7.Text = gr.Cells[3].Text;
        TextBox8.Text = gr.Cells[4].Text;
        TextBox4.Text = gr.Cells[5].Text;
        TextBox5.Text = gr.Cells[6].Text;
        TextBox6.Text = gr.Cells[1].Text;
    }


    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {     // to set the value of the selected item in dropdown
        DropDownList1.SelectedValue = DropDownList1.SelectedItem.Value;
        DropDownList2.SelectedValue = DropDownList2.SelectedItem.Value;


    }



    protected void btnEdit_Click(object sender, EventArgs e)
    { ///<summary> Disabling/Enabling of input fields and button when a certain button is clicked</summary>
        SetEnable(true);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    { ///<summary> Disabling/Enabling of input fields and button when a certain button is clicked</summary>
        SetEnable(false);

        string connetionString;
        SqlConnection cnn;

        connetionString = @"Data Source=******\MSSQL****;Initial Catalog=*****;User ID=****;Password=****";

        cnn = new SqlConnection(connetionString);

        cnn.Open();

        SqlCommand cmd = new SqlCommand("Update TV_LABCASE Set DEPARTMENT_CASE_NUMBER=@DEPARTMENT_CASE_NUMBER,DEPARTMENT_CODE=@DEPARTMENT_NAME,OFFENSE_CODE=@OFFENSE_DESCRIPTION,LAB_CASE=@LAB_CASE,OFFENSE_DATE=@OFFENSE_DATE where CASE_KEY=@CASE_KEY", cnn);
        cmd.Parameters.AddWithValue("@DEPARTMENT_CASE_NUMBER", TextBox1.Text);
        cmd.Parameters.AddWithValue("@DEPARTMENT_NAME", DropDownList1.SelectedValue);
        cmd.Parameters.AddWithValue("@OFFENSE_DESCRIPTION", DropDownList2.SelectedValue);
        cmd.Parameters.AddWithValue("@LAB_CASE", TextBox4.Text);
        cmd.Parameters.AddWithValue("@OFFENSE_DATE", TextBox5.Text);
        cmd.Parameters.AddWithValue("@CASE_KEY", TextBox6.Text);
        cmd.ExecuteNonQuery();
        cnn.Close();
        TextBox7.Text = DropDownList1.SelectedItem.Text;
        TextBox8.Text = DropDownList2.SelectedItem.Text;
        GridView1.DataBind();   

    }

    protected void btnCancel_Click(object sender, EventArgs e)
    { ///<summary> Disabling/Enabling of input fields and button when a certain button is clicked</summary>
        SetEnable(false);
        LoadData();

    }

  ///<summary> Disabling/Enabling of input fields and button when a certain button is clicked</summary>
    private void SetEnable(bool editable)
    {
        btnEdit.Enabled = !editable;
        btnSave.Enabled = editable;
        btnCancel.Enabled = editable;
        TextBox1.Enabled = editable;
        DropDownList1.Enabled = editable;
        DropDownList2.Enabled = editable;
        TextBox4.Enabled = editable;
        TextBox5.Enabled = editable;
        TextBox7.Visible = !editable;
        TextBox8.Visible = !editable;
        DropDownList1.Visible = editable;
        DropDownList2.Visible = editable;


    }

这将意味着您需要api中的最新信息,因此基本上不需要缓存。 您可能还想尝试其他选择,然后看看哪个最适合您 像 const [sendQuery, { data, loading }] = useLazyQuery(GET_DIRECTION, { variables: queryVariables, fetchPolicy: "network-only" }); 一样,您可以在这里enter image description here

中找到更多内容

答案 1 :(得分:0)

根据 docsuseLazyQuery 接受参数 fetchPolicy

const [lazyQuery, { data, loading }] = useLazyQuery(QUERY, {
  fetchPolicy: 'no-cache'
});

fetchPolicy 设置为 no-cache 时,查询的结果不会存储在缓存中。