简体   繁体   English

从子表单组件ReactJS获取道具

[英]Get props from child form component , ReactJS

I am new to React . 我是React新手。

I am writing a component ArrayInput that contains multiple(based on state) input box. 我正在写一个组件ArrayInput ,其中包含多个(基于状态)输入框。

And ArrayInput need to handle each input box's onChange event. 而且ArrayInput需要处理每个输入框的onChange事件。

I hope to get some specific props/attribute (in this case , "index") on these dramatically generated input box 我希望在这些生成的输入框上获得一些特定的道具/属性(在本例中为“索引”)

I search for many posts and docs but can't find the correct way. 我搜索许多帖子和文档,但找不到正确的方法。

I know I can use this.ref[inputBoxRef] (react 14+) to get the actual DOM node , but find it has no "attribute" or "data" when using $(domnode).attr('index') or $(domnode).data('index') . 我知道我可以使用this.ref[inputBoxRef] (反应14+)来获取实际的DOM节点,但是在使用$(domnode).attr('index')$(domnode).data('index')时,发现它没有“属性”或“数据” $(domnode).data('index')

    window.ArrayInput = React.createClass({

            ......other methods

            handleChange:function(ref,event){
                var domInputBox = this.refs[ref];
                //trying to get the index attribute of this input
             }

            render:function(){
                var self = this;
                return (
                    <div className="input-wrapper"  >                               
                        <label>
                            <div>{this.props.label}</div>

                            {
                                this.state.value.map(function(e,i){
                                    return  (                           
                                        <input type="text"
                                            ref={"arrayBox"+i}
                                            key={"arrayBox"+i}
                                            index={i} //custom attribute
                                            value={e}
                                            onChange={self.handleChange.bind(self,"arrayBox"+i)}
                                        />
                                    )
                                })
                            }

                        </label>
                    </div>
                )
            }
    });

You can always use event.target . 您可以始终使用event.target try event.target.index . 尝试event.target.index

Your <input> (with lowercase) is a standard HTML component. 您的<input> (小写)是标准的HTML组件。

Probably best to try and avoid to put custom attributes on these components in react. 最好避免尝试将自定义属性放在这些组件上以作出反应。
Because in effect you are then unnecessarily poluting your DOM with data that could and should be passed within react itself. 因为实际上,您不必要地使用可以并且应该在内部传递的数据污染DOM。 And it is a long trip from react to DOM, to react reading the DOM again to get the data. 从反应到DOM,再做出反应以再次读取DOM以获取数据,这是一个漫长的旅程。

If you want to know the index variable inside handleChange, bind it to the call to handler, like: 如果您想知道handleChange内部的index变量,请将其绑定到对处理程序的调用,例如:

onChange={self.handleChange.bind(self,i)}

and if you change you handler definition to: 并且如果您将处理程序定义更改为:

handleChange:function(element,i){
  console.log(element.value);    // outputs value of element that changed
  console.log(i);                // outputs the index of the element that changed

To get the access the item's value (or any other DOM attribute), as well as the index. 为了获得访问权限,需要使用项目的值(或任何其他DOM属性)以及索引。

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

相关问题 ReactJS - 如何从多个子组件获取表单值 - ReactJS - How to get form value from multiple child component 将道具从父级发送到子级,并在子级组件(ReactJs)中对其进行更新 - Send props from parent to child and update them in child component (ReactJs) 子组件reactjs的动态道具 - Dynamic props to child component reactjs reactjs-如何将数据从子组件转换为道具中传递的按钮的onclick? - reactjs - how to get data from a child component into onclick of a button passed in props? ReactJs 如何获取子组件中的特定道具数据 - ReactJs how to get specific props data in child component Reactjs - 使用子组件中的setState从props设置State - Reactjs - Setting State from props using setState in child component 如何将道具从子组件传递到父组件到 ReactJS 中的另一个子组件? - How to pass props from child component to parent component to another child component in ReactJS? ReactJs子组件未通过道具更新 - ReactJs Child Component is not getting updated through Props ReactJS 表单:父组件状态更新,但子字段被清空,道具保持不变 - ReactJS Form: Parent Component state updates, but Child's field gets emptied & props remain unchanged 如何通过在 ReactJS 中将 props 从父组件传递到子组件来初始化状态? - How can i initialize state by passing props from parent component to child component in ReactJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM