javascript - react-query中如何使用useMutation的响应来显示数据?

标签 javascript reactjs react-hooks react-query

我正在使用 react-query 的 useMutation() 来发布数据,我能够成功发布数据。
但是,我在成功发布我想要使用该响应并将其显示在组件中的数据后返回的响应
为此,我正在使用状态,并且我在 useMutation() 钩子(Hook)中的 onSuccess 函数内设置状态,但它给了我错误
错误:
TypeError:无法读取未定义的属性“tidpWarnings”
在 Object.onSuccess (UpdateMIDPPage.jsx:80)
在突变.js:86

下面是我的代码供引用

const UpdateMIDPPage = ({ open, handleClose }) => {
  const classes = useStyles()
  const [tidpFiles, setTidpFiles] = useState([])
  const [reportFile, setReportFile] = useState('')
  const [tidpWarnings, setTidpWarnings] = useState([])
  const [reportWarnings, setReportWarnings] = useState([])
  const [toggleVisibleIcon, setToggleVisibleIcon] = useState(true)

  const { mutate, data, isSuccess, isLoading, isIdle, isError } = useMutation(
    async (body) => {
      const resp = await axios.post('http://localhost:4000/api/v1/midp/update', body)
      return resp.data
    },
    {
      onSuccess: async (data) => {    
        console.log(data)
        setTidpWarnings(data.warnings1.tidpWarnings)
        setReportWarnings(data.warnings1.reportWarnings)
      },
      onError: async (error) => {
        console.log(error)
      },
    },
  )
  const handleUpdateMIDP = () => {
    const body = {
      projectId,
      tidpFiles,
      reportFile,
    }
    mutate(body)
    // also tried this doesnt work eiter
    // mutate(body, {
    //   onSuccess: async () => {
    //     setTidpWarnings(data.warnings1.tidpWarnings)
    //     setReportWarnings(data.warnings1.reportWarnings)
    //   },
    // })
  }

  return (
    <div className={classes.container}>
      <div className={classes.action}>
        <div>       
          <Button
            onClick={handleUpdateMIDP}
            className={classes.updateBtn}
            variant='contained'
            disableElevation
            startIcon={<CheckIcon />}
            disabled={tidpFiles === [] ? true : reportFile === '' ? true : isSuccess ? true : false}
          >
            {isIdle
              ? 'Generate MIDP'
              : isLoading
              ? 'Processing...'
              : isSuccess
              ? 'Processed!'
              : isError
              ? 'Error'
              : null}
          </Button>
        </div>
        <div>         
          <Tooltip title='Toggle upload section'>
            <IconButton onClick={() => setToggleVisibleIcon(!toggleVisibleIcon)}>
              {toggleVisibleIcon ? <VisibilityIcon /> : <VisibilityOffIcon />}
            </IconButton>
          </Tooltip>
        </div>
      </div>
      {toggleVisibleIcon ? (
        <UploadSection
          reportFile={reportFile}
          setReportFile={setReportFile}
          tidpFiles={tidpFiles}
          setTidpFiles={setTidpFiles}
        />
      ) : null}
      {isLoading ? <div>loading ....</div> : null}
      {isSuccess ? <WarningSection tidpWarnings={tidpWarnings} reportWarnings={reportWarnings} /> : null}
    </div>
  )
}
如果我在 onSuccess 中注释 setState 代码,一切正常。
这是供引用的 APi 响应
{status: "success", data: {…}}
data:
ignoreFiles: (2) ["test1.xlsx", "test5.xlsx"]
warnings1: {tidpWarnings: Array(3), reportWarnings: Array(0)}
__proto__: Object
status: "success"
__proto__: Object
请帮我解决这个问题,谢谢

最佳答案

For this I am using state, and i am setting state inside onSuccess function in useMutation() hook

data从 useMutation 返回的将包含从 api 返回的响应,因此不需要添加额外的状态管理

关于javascript - react-query中如何使用useMutation的响应来显示数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67097382/

相关文章:

reactjs - 当父状态改变时组件卸载

javascript - 有没有办法结合 Webpack 模块来减小文件大小?

javascript - jQuery 单选按钮检查添加类,否则如果单选按钮未选中则删除类?

当从 cookie 中警告加密字符串时,Javascript + 字符被删除

javascript - 将数组的元素作为 React 中对象的一部分推送

javascript - 如何可靠地测量 React 组件的弹出框和标注的计算位置?

reactjs - 用 Jest 运行测试时的可选链接问题

javascript - 有条件的 onclick 函数在没有点击的情况下触发?

javascript - 何时将 'classes' 与 'className' 与 Material UI 一起使用?

reactjs - 如何将 useMemo 与外部 API 一起使用?