React Native问题:插入空行

发布于 2025-01-11 11:38:28 字数 1084 浏览 0 评论 0原文

在 React 中测试了一些东西,并发现我的一些逻辑存在一些问题。我试图从表单中的输入中获取值,然后在提交该表单时我想获取该输入值对象并将它们添加到我的工厂状态中。当我控制台日志时,我得到状态值,但植物状态为空。任何帮助表示赞赏。

谢谢

      const [plant, setPlant] = useState();
          const [TypeofPlant, setType] = useState('')
          const [Phase, setPhase] = useState('')
          const [Days, setDay] = useState(null)
          const [Start, setStart] = useState(0)
          const [End, setEnd] = useState(0)
        
  const handleSubmit = async (e) => {

        e.preventDefault()

          setPlant({...plant, 
            TypeofPlant: TypeofPlant,
            Phase: Phase,
            Days: Days,
            Start: Start,
            End: End,
        
       
        
        })
       
                DataService.addNewPlant(plant).then(() => {
      
                    })
                   },
                    (error) => {
                        console.log(error)
                    }
                );

数据服务:

  addNewPlant(data){
        return http.post('/plants/add',data)
        }

Testing some things in React and having some issues with some of my logic. I am trying to get value from inputs in a form then on submitting that form I want to take that object of input values and add them to my plant state. When I console log I get the state values but then the plant state is empty. Any help is appreciated.

Thank you

      const [plant, setPlant] = useState();
          const [TypeofPlant, setType] = useState('')
          const [Phase, setPhase] = useState('')
          const [Days, setDay] = useState(null)
          const [Start, setStart] = useState(0)
          const [End, setEnd] = useState(0)
        
  const handleSubmit = async (e) => {

        e.preventDefault()

          setPlant({...plant, 
            TypeofPlant: TypeofPlant,
            Phase: Phase,
            Days: Days,
            Start: Start,
            End: End,
        
       
        
        })
       
                DataService.addNewPlant(plant).then(() => {
      
                    })
                   },
                    (error) => {
                        console.log(error)
                    }
                );

DataService:

  addNewPlant(data){
        return http.post('/plants/add',data)
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰認得朕 2025-01-18 11:38:28

尝试在 DataService 块之外对工厂进行控制台。

Try to console plant outside of DataService block.

怕倦 2025-01-18 11:38:28

Response.json() 方法接口采用响应流并读取它完成。它返回一个承诺,该承诺将正文文本解析为 JSON 的结果进行解析。

更新代码如下

....
DataService.addNewPlant(data).then(res => {
            const response = res.json();
            setPlant({
                TypeofPlant: response.data.TypeofPlant,
                Phase: response.data.Phase,
                Days: response.data.Days,
                Start: response.data.Start,
                End: response.data.End,
            })
            console.log(response.data);}
....

The Response.json() method interface takes a Response stream and reads it to completion. It returns a promise which resolves with the result of parsing the body text as JSON.

Update the code like below

....
DataService.addNewPlant(data).then(res => {
            const response = res.json();
            setPlant({
                TypeofPlant: response.data.TypeofPlant,
                Phase: response.data.Phase,
                Days: response.data.Days,
                Start: response.data.Start,
                End: response.data.End,
            })
            console.log(response.data);}
....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文