简体   繁体   English

React,Reactstrap-输入与输入,表单控制类阻止值提交,返回未定义

[英]React, Reactstrap - Input vs input, form-control class prevents values from submitting, returns undefined

I am working on an basic React CRUD App and I came across a strange issue. 我正在开发基本的React CRUD应用程序,但遇到了一个奇怪的问题。 Using Reactstrap Input Component or the bootstrap class="form-control" the input values are undefined onSubmit. 使用Reactstrap输入组件或bootstrap class =“ form-control” ,输入值在onSubmit上未定义。 If I leave the elements as a standard html input the values submit just fine? 如果我将元素保留为标准html输入,则提交值就好了吗? I cannot figure out why this is happening. 我不知道为什么会这样。 I put together this demo that illustrates the issue. 我整理了演示该问题的演示。

Code Sandbox Snippet <- Live Demo 代码沙箱摘要 <-实时演示

On the AddName.js file I have one Input and one input on submit you can see that the first name submits but the description does not. AddName.js文件我有一个输入和提交,你可以看到第一个名字提交,但描述没有一个输入

 <FormGroup> <input placeholder="First Name" ref={nameInput => (this.nameInput = nameInput)} /> </FormGroup> <FormGroup> <Input placeholder="Description" ref={descriptionInput => (this.descriptionInput = descriptionInput)} /> </FormGroup> 

Can anyone explain why this is happening? 谁能解释为什么会这样? Is it possible how I have the refs on the inputs? 输入的参考资料可能如何?

Before utilizing 3rd party packages, you should read into using Controlled Components strictly through React. 在使用第三方包之前,您应该严格通过React阅读使用受控组件 Then, once you understand the flow, incorporate the packages. 然后,一旦您了解了流程,请合并这些软件包。

I've rewritten your project to include all of the above, as well as written notes describing what I changed and why. 我已经重写了您的项目,以包括上述所有内容,以及描述我更改内容和原因的书面说明。 There are also a few tricks and shortcuts I've used to simplify the code, such as: setState callback , ES6 Object Deconstruction and Fat Arrow Functions . 我还使用了一些技巧和捷径来简化代码,例如: setState回调ES6对象解构Fat Arrow函数

Working example : https://codesandbox.io/s/l9m0vor4wq 工作示例https : //codesandbox.io/s/l9m0vor4wq

You should only need to use refs for obtaining a DOM element or a class instance for UI manipulation, instead of obtaining a data value. 您只需要使用refs来获取用于UI操作的DOM元素或类实例,而不是获取数据值。 For example, utilizing a ref to draw attention to a DOM element via using its focus() method or removing attention from it via its blur() method. 例如,通过使用ref通过其focus()方法吸引对DOM元素的关注,或通过其blur()方法从DOM元素吸引注意力。 In your case, we would only use the synthetic event instead of a ref. 在您的情况下,我们将仅使用合成event而不是引用。

On a side note, be careful about using the name as a key in <PersonCard/> , because multiple users can share the same name value. 另外,请小心使用name作为<PersonCard/>的键,因为多个用户可以共享相同的name值。 This will cause React to complain that two keys are the same, and cause issues if you ever try to remove, resort, or filter the names list. 这将导致React抱怨两个键相同,并且如果您尝试删除,重新排序或过滤名称列表,则会导致问题。 To keep it simple, I instead used the key provided by the map function. 为了简单起见,我改用了map函数提供的key While this approach works, it's not the best solution. 尽管此方法有效,但这不是最佳解决方案。 Ideally, each person should have an id property with a UUID . 理想情况下,每个人都应具有一个带有UUIDid属性。 This ensures all of your keys will be unique. 这样可以确保所有键都是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM