Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 遍历子对象';s子项,并将函数添加到所有输入,同时保持其他子项不变_Javascript_Reactjs - Fatal编程技术网

Javascript 遍历子对象';s子项,并将函数添加到所有输入,同时保持其他子项不变

Javascript 遍历子对象';s子项,并将函数添加到所有输入,同时保持其他子项不变,javascript,reactjs,Javascript,Reactjs,我已经试着让这个工作了一段时间,现在不知道如何做以下。“我的表单”组件具有包含常规html标记和输入的子级。如果子项是输入,我想添加attachToForm和detachFromForm函数。如果不是输入,我想继续遍历子元素,以确保元素没有子输入字段。无论元素是否是输入,我仍然希望它出现在我的页面上,我只想将函数添加到输入中 问题是我只能让我的函数只返回输入,删除标签和标题。我知道这是因为我只向newChildren添加带有输入的元素,但是如果我在elseif部分中推其他元素,我会得到重复的元素

我已经试着让这个工作了一段时间,现在不知道如何做以下。“我的表单”组件具有包含常规html标记和输入的子级。如果子项是输入,我想添加
attachToForm
detachFromForm
函数。如果不是输入,我想继续遍历子元素,以确保元素没有子输入字段。无论元素是否是输入,我仍然希望它出现在我的页面上,我只想将函数添加到输入中

问题是我只能让我的函数只返回输入,删除标签和标题。我知道这是因为我只向newChildren添加带有输入的元素,但是如果我在elseif部分中推其他元素,我会得到重复的元素,我可以想出另一种方法。我不确定我是否不了解基本的JS或是有一个大脑缺口

React.Children.forEach(children, function(child) {
        var current = child;
        if (child.props && child.props.name) {
            this.newChildren.push(React.cloneElement(child, {
                detachFromForm: this.detachFromForm,
                attachToForm: this.attachToForm,
                key: child.props.name
            }));
        } else if (child.props && child.props.children){
            this.newChildren.push(child);
            this.registerInputs(child.props.children);
        } else {
            *need to keep track of parent elements and elements that do not have inputs
        }
    }.bind(this));
编辑:不确定是否需要,但这是im遍历的示例和表单

return ( 
            <Modal className="_common-edit-team-settings" title={`Edit ${this.props.team.name}`} isOpen={this.props.modalIsOpen && this.props.editTeamModal} onCancel={this.props.toggleEditTeamModal} backdropClosesModal>

                <Form onSubmit={this.saveChanges}>      
                    <FormSection className="edit-team-details" sectionHeader="Team Details">
                        <FormField label="Name">
                            <Input name="name" value={this.state.values.name} onChange={this.handleInputChange} type="text" placeholder={this.props.team.name}/>
                        </FormField>
                        <FormField label="Mission">
                            <Input name="mission" value={this.state.values.mission} onChange={this.handleInputChange} type="text" placeholder={this.props.team.kitMission || 'Kit Mission'} multiline />
                        </FormField>
                    </FormSection>

                    <FormSection className="privacy-settings" sectionHeader="Privacy Settings">
                        <FormField label="Included in global search results" >
                            <SlideToggle name="globalSearch" defaultChecked={this.state.values.globalSearch} onChange={this.handleCheckedChange} type="checkbox" />
                        </FormField>
                        <FormField label="Accessible by anyone" >
                            <SlideToggle name="public" defaultChecked={this.state.values.public} onChange={this.handleCheckedChange} type="checkbox" />
                        </FormField>
                        <FormField label="Secured with WitCrypt" >
                            <SlideToggle name="witcryptSecured" defaultChecked={this.state.values.witcryptSecured} onChange={this.handleCheckedChange} type="checkbox" />
                        </FormField>
                    </FormSection>
                    <FormSection sectionHeader="Participants">
                        {participantsList}
                        <div id="add-participant" className="participant" onClick={this.toggleAddParticipantModal}>
                            <span className="participant-avatar" style={{backgroundImage:'url(/img/blue_add.svg)'}}></span>
                            <span>Add a Participant</span>
                            <span className="add-action roll"><a></a></span>
                        </div>
                    </FormSection>
                    <Button type="hollow-primary" size="md" className="single-modal-btn" block submit>Save</Button>
                </Form>


                <AddParticipant people={this.props.people} toggleAddParticipantModal={this.props.toggleAddParticipantModal} modalIsOpen={this.props.modalIsOpen} toggleAddParticipantModal={this.toggleAddParticipantModal} addParticipantModal={this.state.addParticipantModal} />
            </Modal>
        );

根据错误消息判断,不可变的
prop
对象存在问题。从React 0.14开始,
prop
被“冻结”:

props
对象现在已冻结,因此不再支持在创建组件元素后更改props。在大多数情况下,应使用
React.cloneElement
。此更改使您的组件更易于推理,并支持上面提到的编译器优化

因此,在代码中的某个地方,您尝试扩展导致错误的
prop
对象

您可以将
prop
交互的不同部分包装成
try..catch
结构,它将为您指出确切的问题所在

registerInputs: function (children) {

    React.Children.forEach(children, function (child) {

      if (child.props.name) {

        child.props.attachToForm = this.attachToForm;
        child.props.detachFromForm = this.detachFromForm;
      }

      if (child.props.children) {
        this.registerInputs(child.props.children);
      }
    }.bind(this));
  }