Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何使用Axios和Typescript将初始值设置为Formik_Reactjs_Axios_Formik - Fatal编程技术网

Reactjs 如何使用Axios和Typescript将初始值设置为Formik

Reactjs 如何使用Axios和Typescript将初始值设置为Formik,reactjs,axios,formik,Reactjs,Axios,Formik,我不熟悉使用React、Formik和Axios,不知道如何通过数据库调用设置表单的初始值。我尝试了下面的代码片段,但没有成功。我在网上找不到任何关于如何执行此操作的typescript示例 async function getInitialValues() { try { const response = await axios.get('http://localhost:53132/api/test'); //console.log

我不熟悉使用React、Formik和Axios,不知道如何通过数据库调用设置表单的初始值。我尝试了下面的代码片段,但没有成功。我在网上找不到任何关于如何执行此操作的typescript示例

async function getInitialValues() {
      try {
            const response = await axios.get('http://localhost:53132/api/test');
            //console.log(response);


            return {
                Division_Id: response.Divison_Id,
                Year: response.Year,
                Cost: response.Cost
            }

            //console.log(InitialValues);

            //return InitialValues;


          } catch (error) {
            console.error(error);
          }
    }


<Formik initialValues={getInitialValues()}...


在本例中,您需要使用useffect钩子在挂载时发出网络请求。然后在这里使用useState钩子将这些值保存到state,但是您可以使用Redux或您正在使用的任何状态管理工具

function NewForm() {
  const [initialValues, setInitialValues] = useState();

  useEffect(() => {
    getInitialValues().then(res => setInitialValues(res);
  }, []);

  return initialValues ? 
    <Formik initialValues={initialValues}>content</Formik> :
    <span>loading...</span>;
}

在本例中,您需要使用useffect钩子在挂载时发出网络请求。然后在这里使用useState钩子将这些值保存到state,但是您可以使用Redux或您正在使用的任何状态管理工具

function NewForm() {
  const [initialValues, setInitialValues] = useState();

  useEffect(() => {
    getInitialValues().then(res => setInitialValues(res);
  }, []);

  return initialValues ? 
    <Formik initialValues={initialValues}>content</Formik> :
    <span>loading...</span>;
}

这是因为您缺少useEffect中的依赖项。如果只希望在页面呈现时运行一次,请添加一个空数组作为useEffect的dependencysecond参数

   function NewForm() {
      const [initialValues, setInitialValues] = useState();

      useEffect(() => {
        getInitialValues().then(res => setInitialValues(res);
      }, []);

      return initialValues ? 
        <Formik initialValues={initialValues}>content</Formik> :
        <span>loading...</span>;
    }

这是因为您缺少useEffect中的依赖项。如果只希望在页面呈现时运行一次,请添加一个空数组作为useEffect的dependencysecond参数

   function NewForm() {
      const [initialValues, setInitialValues] = useState();

      useEffect(() => {
        getInitialValues().then(res => setInitialValues(res);
      }, []);

      return initialValues ? 
        <Formik initialValues={initialValues}>content</Formik> :
        <span>loading...</span>;
    }

你有什么错误吗?你能添加更多的细节吗?你有什么错误吗?你能添加更多的细节吗?我试着更新我的代码,但是Stackoverflow不允许我发布整个文件。我已经修改了代码,可以在下面的链接中找到。该应用程序乱七八糟,只是不停地打电话给该服务。。。我尝试更新代码,但Stackoverflow不允许我发布整个文件。我已经修改了代码,可以在下面的链接中找到。该应用程序乱七八糟,只是不停地打电话给该服务。。。谢谢大家的帮助!它终于点击并让它工作了!谢谢大家的帮助!它终于点击并让它工作了!