我是全新的反应。我已经正式完成用头撞墙了。我就是想不通。这是我的情况:
我正在尝试将 API 调用的结果放入表中。我已调用 API 工作并返回结果。我被困在如何使用返回的数据更新我的数组。完成后,我可以用数据填充表格(至少在逻辑上这是我的大脑告诉我的方式)。
初始表单状态设置:
const initialFormState = {
fileTypeId : '',
companyMasterId: '',
investmentCompanyId: '',
startDate: '',
endDate: '',
result: '',
fileLogs: []
}
Run Code Online (Sandbox Code Playgroud)
以上所有字段都是表单\数据库中的字段。API 调用采用这些参数来调用存储过程,该过程根据搜索参数返回结果集。fileLogs[] 是我想放置返回数据的地方。我不确定是否需要将它从这个设置中移出并使用 useState 作为一个单独的东西?
减速器初始化:
const [formState, dispatch] = useReducer (formReducer, initialFormState)
Run Code Online (Sandbox Code Playgroud)
减速机设置
formReducer.js
import actionTypes from "./actionTypes"
const formReducer = (state, action) => {
switch (action.type) {
case actionTypes.handle_input_text:
return {
//using the spread operator (…state) to copy across all the properties and values from the state object.
//then we can …Run Code Online (Sandbox Code Playgroud)