微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Axios 使用 Formik 发布数据时的挂钩调用无效

如何解决使用 Axios 使用 Formik 发布数据时的挂钩调用无效

总结问题

我的代码的目标是将表单数据发布到我的 REST API:/api/profiles/<:id>。

我使用 Formik 处理所有需要用表单完成的工作,并使用 axios 获取当前用户的 id 然后填写正确的 REST API url,以及执行对该 REST API 的 POST 请求。实际结果是一条错误消息,指出我使用了无效的钩子调用。这是错误图片

Error Image

描述您尝试过的内容

我去了 React 网站,那里提到了几种调试情况的方法https://reactjs.org/warnings/invalid-hook-call-warning.html

我发现我写的代码实际上有两份 react 包。因此,我转到此堆栈溢出以查看如何修复它:Duplicate ReactJS import issue when using npm link to test component before publishing as npm package 但是当我按照接受的答案进行操作时,我收到一条错误消息,指出该操作被我的操作系统 (macOS) 拒绝。接受的答案还谈到拥有一个本地图书馆,但我认为我从未使用过它,所以我觉得我的问题的解决方案在于我的代码中的某处。

显示一些代码

  postText: Yup.string().required("required"),message: Yup.string().required("required")
});



function getProfile(){
  const [profiles,setProfiles] = useState([])

  useEffect(() => {
      const p = getProfile(window.REP_LOG_APP_PROPS.user_id)
      const api = axios.create({
        baseURL: 'http://127.0.0.1:8000/api/profiles/' + p.id
      })
      axios.get(api)
          .then(res =>{
              console.log(res)
              setProfiles(res.data)
          })
          .catch(err => {
              console.log(err)
          })
  },[])
  return (
                  profiles

  )
}

export default function CreatePost()  {
  /* Server State Handling */
  const [serverState,setServerState] = useState();
  const handleServerResponse = (ok,msg) => {
    setServerState({ok,msg});
  };
  const p = getProfile(window.REP_LOG_APP_PROPS.user_id)
  const api = axios.create({
    baseURL: 'http://127.0.0.1:8000/api/profiles/' + p.id
  })
  const handleOnSubmit = (values,actions) => {
    axios({
      method: "POST",url: api,data: values
    })
      .then(response => {
        actions.setSubmitting(false);
        actions.resetForm();
        handleServerResponse(true,"Thanks!");
      })
      .catch(error => {
        actions.setSubmitting(false);
        handleServerResponse(false,error.response.data.error);
      });
  };
  return (
    <div>
      <h1>Create a Post</h1>
      <Formik
        initialValues={{ postText: "Post something inspiring..."}}
        onSubmit={handleOnSubmit}
        validationSchema={formSchema}
      >
        {({ isSubmitting }) => (
          <Form id="fs-frm" novalidate>
            <label htmlFor="postText">This is the label here</label>
            <Field id="postText" name="postText" component="textarea" />
            <ErrorMessage name="postText" className="errorMsg" component="p" />
            <button type="submit" disabled={isSubmitting}>
              Post
            </button>
            {serverState && (
              <p className={!serverState.ok ? "errorMsg" : ""}>
                {serverState.msg}
              </p>
            )}
          </Form>
        )}
      </Formik>
    </div>
  );

};```

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?