【问题标题】:How to save a nested object in react js form input using hooks?如何使用钩子在 React js 表单输入中保存嵌套对象?
【发布时间】:2021-12-10 16:11:11
【问题描述】:

在这里,我试图将 category_id 保存到数据库中,该数据库是类别对象的一部分,例如 目的:{ 类别ID:“” } 但是这样做会抛出错误“分离的实体传递给持久化”。这很混乱。请helo

            <FormGroup>
                <label for="category_id">category_id</label>
                <Input
                    placeholder="Enter category id"
                    id="category_id"
                    name="category_id"
                    type="textarea"
                    onChange={(e) => {
                      setProduct({ ...product, category: { categoryId: e.target.value } })
                    }}
                />
            </FormGroup>

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: reactjs spring-boot axios react-hooks


【解决方案1】:

在 onChange 函数中调用一个单独的函数并将值作为参数传递

onChange={(e)=>sendData(e.target.value)}

let products = {}
const sendData = (value)=>{
setInputValue(value)
productDetails = { ...products, catId:inputValue}
setProd(products)
}

【讨论】:

    猜你喜欢
    • 2021-07-01
    • 2021-12-29
    • 1970-01-01
    • 2019-05-02
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    相关资源
    最近更新 更多