axios 更改React中的文本输入值

hgc7kmma  于 2022-11-23  发布在  iOS
关注(0)|答案(1)|浏览(144)

我试图更新输入文本的值后,HTTP POST响应是从服务器收到。PFB我的代码细节,让我知道我做错了什么,在这里或指导我通过任何演示。

import React, { Fragment, useState, changeState, setState, useEffect, useContext } from 'react';
import { AuthContext, Onboarding, Form, Message, useAPI } from 'components/lib';
import Axios from 'axios';    

function Welcome() {
      const context = useContext(AuthContext);
      const postData = { user_id: context.user.user_id }
      const [items, setItems] = useState({
        business_name: "ABC"
      });
      useEffect( () =>  {
        const singleData = async () => {
          let response = await Axios.post( '/api/getbusiness', postData)
                 .then(res => {
                  setItems({
                    business_name: res.data.data.business_name
                  });
                })
                .catch((error) => {
                  console.log(error);
                })
        }
        singleData()
      },[])
    
      return (
        <Form
          data={
            business_name: {
              label: 'Business Name',
              type: 'text',
              required: true,
              errorMessage: 'Please enter business name',
              value: `${ items.business_name }`
            }
          }}
          url='/api/onboarding/1'
          method='POST'
          buttonText='Save'
          callback={ res => {
            console.log(res);
          }}
          />
    
      )
    }

在页面加载时,输入显示值“ABC”,但在获得HTTP响应setItems后,输入字段中的值不会更新。

qv7cva1a

qv7cva1a1#

你有res.data.data.business_name,而它应该是res.data.business_name。同样在你的表单input中,你需要添加一个defaultValue={items.business_name}属性来显示你的axios调用的响应。

相关问题