使用setState

时间:2018-04-23 01:35:11

标签: javascript reactjs typescript setstate typesafe

所以我最近发现事件处理程序的回调对渲染性能不利。叹。 (https://reactjs.org/docs/handling-events.html

我试图通过在类方法中抓取事件的属性而不是执行类似的操作来注意这一点:onClick={({value}) => this.setState({"someProperty" as keyof State: value})}等。

但是现在我无法在回调中明确声明类型安全,我试图对它进行动态调整,并且我的类型检查器可以使用下面的代码,但是我怎么能让它抱怨输入元素有{ {1}}属性不是name

keyof State

1 个答案:

答案 0 :(得分:1)

您明确地将public partial class EditAccount : System.Web.UI.Page { SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True"); private readonly object MessageBox; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SqlCommand com; string str; conn.Open(); str = "SELECT Address.HouseNumber, Address.AddressLine1, Address.AddressLine2, Address.City, Address.PostCode, CardDetails.CardDetailsID, CardDetails.NameOnCard, CardDetails.NameOfCard, CardDetails.CardNumber, CardDetails.CardExpiryDate, CardDetails.CVV, AddressType.AddressTypeDescription, Member.MemberID, Member.MemberName, Member.Phone, Member.Email, Member.Username FROM Address FULL JOIN AddressType ON Address.AddressTypeID = AddressType.AddressTypeID FULL JOIN Member ON AddressType.MemberID = Member.MemberID FULL JOIN CardDetails ON Member.MemberID = CardDetails.MemberID WHERE Member.Email = '" + Session["Email"] + "'"; com = new SqlCommand(str, conn); SqlDataReader reader = com.ExecuteReader(); if (reader.Read()) { TxtEName.Text = reader["MemberName"].ToString(); LblEUser.Text = reader["Username"].ToString(); TxtEEmail.Text = reader["Email"].ToString(); TxtEPhone.Text = reader["Phone"].ToString(); TxtEType.Text = reader["AddressTypeDescription"].ToString(); TxtEHouse.Text = reader["HouseNumber"].ToString(); TxtEA1.Text = reader["AddressLine1"].ToString(); TxtEA2.Text = reader["AddressLine2"].ToString(); TxtECity.Text = reader["City"].ToString(); TxtEPostcode.Text = reader["PostCode"].ToString(); TxtENameOf.Text = reader["NameOfCard"].ToString(); TxtENameOn.Text = reader["NameOnCard"].ToString(); TxtECardNo.Text = reader["CardNumber"].ToString(); TxtEExpDate.Text = reader["CardExpiryDate"].ToString(); TxtECVV.Text = reader["CVV"].ToString(); reader.Close(); conn.Close(); } } } protected void BtnSave_Click(object sender, EventArgs e) { conn.Open(); SqlDataAdapter str = new SqlDataAdapter ("SELECT Address.HouseNumber, Address.AddressLine1, Address.AddressLine2, Address.City, Address.PostCode, CardDetails.NameOnCard, CardDetails.NameOfCard, CardDetails.CardNumber, CardDetails.CardExpiryDate, CardDetails.CVV, AddressType.AddressTypeDescription, Member.MemberName, Member.Phone, Member.Email, Member.Username FROM Address FULL JOIN AddressType ON Address.AddressTypeID = AddressType.AddressTypeID FULL JOIN Member ON AddressType.MemberID = Member.MemberID FULL JOIN CardDetails ON Member.MemberID = CardDetails.MemberID WHERE Member.Email = '" + Session["Email"] + "'",conn); DataSet ds = new DataSet(); str.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { SqlCommand updateCommand = new SqlCommand("UPDATE Member SET MemberName = '" + TxtEName.Text + "', Email = '" + TxtEEmail.Text + "', Phone = '" + TxtEPhone.Text + "'", conn); SqlCommand updateCommand1 = new SqlCommand("UPDATE Address SET HouseNumber ='" + TxtEHouse.Text + "', AddressLine1 = '" + TxtEA1.Text + "', AddressLine2 = '" + TxtEA2.Text + "', City = '" + TxtECity.Text + "',PostCode = '" + TxtEPostcode.Text + "'", conn); SqlCommand updateCommand2 = new SqlCommand("UPDATE AddressType SET AddressTypeDescription = '" + TxtEType.Text + "'", conn); SqlCommand updateCommand3 = new SqlCommand("UPDATE CardDetails SET NameOnCard = '" + TxtENameOn.Text + "', NameOfCard = '" + TxtENameOf.Text + "', CardNumber = '" + TxtECardNo.Text + "', CardExpiryDate = '" + TxtEExpDate.Text + "', CVV = '" + TxtECVV.Text + "'", conn); updateCommand.ExecuteNonQuery(); updateCommand1.ExecuteNonQuery(); updateCommand2.ExecuteNonQuery(); updateCommand3.ExecuteNonQuery(); Response.Write("<script>alert('Changes saved')</script>"); } else { SqlCommand insertCommand = new SqlCommand("INSERT INTO AddressType (AddressTypeDescription) VALUES (@AddressTypeDescription)", conn); insertCommand.Parameters.AddWithValue("@AddressTypeDescription", TxtEType.Text); insertCommand.ExecuteNonQuery(); SqlCommand insertCommand1 = new SqlCommand("INSERT INTO Address (HouseNumber, AddressLine1, AddressLine2, City, PostCode) VALUES (@HouseNumber, @AddressLine1, @AddressLine2, @City, @PostCode)", conn); insertCommand1.Parameters.AddWithValue("@HouseNumber", TxtEHouse.Text); insertCommand1.Parameters.AddWithValue("@AddressLine1", TxtEA1.Text); insertCommand1.Parameters.AddWithValue("@AddressLine2", TxtEA2.Text); insertCommand1.Parameters.AddWithValue("@City", TxtECity.Text); insertCommand1.Parameters.AddWithValue("@PostCode", TxtEPostcode.Text); insertCommand1.ExecuteNonQuery(); SqlCommand insertCommand2 = new SqlCommand("INSERT INTO CardDetails (NameOnCard, NameOfCard, CardNumber, CardExpiryDate, CVV) VALUES (@NameOnCard, @NameOfCard, @CardNumber, @CardExpiryDate, @CVV)", conn); insertCommand2.Parameters.AddWithValue("@NameOnCard", TxtENameOn.Text); insertCommand2.Parameters.AddWithValue("@NameOfCard", TxtENameOf.Text); insertCommand2.Parameters.AddWithValue("@CardNumber", TxtECardNo.Text); insertCommand2.Parameters.AddWithValue("@CardExpiryDate", TxtEExpDate.Text); insertCommand2.Parameters.AddWithValue("@CVV", TxtECVV.Text); insertCommand2.ExecuteNonQuery(); Response.Write("<script>alert('Changes saved')</script>"); } conn.Close(); } } 属性转换为name的键,因此编译器不会抱怨是正常的。

如果您需要类型安全,我会使用闭包代替:

State