我正在寻找如何正确删除操作的最佳做法。
例如我的减速机中有一系列照片。 其中一项功能是选择要删除的功能。 我决定存储在反应状态下选择的项目。我存储了ID数组。
因此,在删除操作后,我必须从redux存储中删除照片并清除处于反应状态的所选数组。
我的建议是订阅承诺并执行以下操作:
this.props.deletePhotos(selected)
.then(() => {
this.setState({ selected: [] })
})
.catch((err) => {
console.log(err)
})
但是有人说这是一个不好的做法。这是真的吗?如何以正确的方式改变它?
答案 0 :(得分:0)
您需要遵守的主要原则是您的州应始终有效。通过使用承诺,您将删除照片,呈现无效状态,然后更新反应状态。即使这样的渲染确实无关紧要,但这并不是很好的做法。
您可能无法在承诺中设置状态:
Test Name: TestCase1
Test FullName: Automation.Excel_TC_1.TestCase1
Test Source: C:\Safety_dan\-localRepoVisualStudioC#\Automation\Excel_TC_1.cs : line 32
Test Outcome: Failed
Test Duration: 0:00:00.005
Result StackTrace:
at System.Data.OleDb.OleDbServicesWrapper.GetDataSource(OleDbConnectionString constr, DataSourceWrapper& datasrcWrapper)
at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.OleDb.OleDbConnection.Open()
at Automation.ExcelReader.ReadExcelData(String excelFile, String sheetname) in C:\Safety_dan\-localRepoVisualStudioC#\Automation\Excel_TC_1.cs:line 27
at Automation.Excel_TC_1.<get_BudgetData>d__2.MoveNext() in C:\Safety_dan\-localRepoVisualStudioC#\Automation\Excel_TC_1.cs:line 20
at NUnit.Framework.TestCaseSourceAttribute.GetTestCasesFor(IMethodInfo method) in C:\src\nunit\nunit\src\NUnitFramework\framework\Attributes\TestCaseSourceAttribute.cs:line 177
Result Message: System.InvalidOperationException : The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
或者,如果你想做一些比清除更复杂的事情,你可以使用const selected = this.state.selected
this.props.deletePhotos(selected)
.catch((err) => {
console.log(err)
})
this.setState({selected: []})
并修改状态,如果相关的道具被改变,可能来自redux。
另一个选择是,如果你完成了输入,你只需要在下一个渲染中渲染组件,状态就会被自动清除,因为它将被卸载。