简体   繁体   English

React AppendChild组件不起作用

[英]React AppendChild Component doesn't work

I've been looking everywhere and cannot find a solution to this. 我到处都在寻找,找不到解决方案。

I'm simply trying to do the following: 我只是尝试执行以下操作:

import ComponentOne from '../components/component-one'
import ComponentTwo from '../components/component-two'

class Home extends Component {
    constructor( props ) {
        // So I can dynamically call a Component, found no other way
        this.components = {
            ComponentOne: <ComponentOne />,
            ComponentTwo: <ComponentTwo />
        }
    }

    [...code removed for brevity...]

    _appendStep( step ) {
        var component = React.cloneElement(this.components[step])
        this.steps.appendChild( component )
    }
}

This seems pretty simple to me. 对我来说,这似乎很简单。 I have 我有

<div className="recipe-steps" ref={(ref) => this.steps = ref}></div>

that I need to dynamically appendChild components too. 我也需要动态appendChild组件。 The thing is, the "steps" I append to this <div> absolutely need to be one of the components I've created, needs to allow me to add multiple component children, and even duplicate (that's why I'm using React.cloneElement() ) Components. 问题是,我添加到此<div>的“步骤”绝对需要成为我创建的组件之一,需要允许我添加多个组件子级甚至重复(这就是为什么我使用React.cloneElement()的原因) React.cloneElement() )组件。

Once I have all the "steps" appended, a later process will then parse each and every step to determine how to run a recipe. 一旦附加了所有“步骤”,随后的过程将解析每个步骤,以确定如何运行配方。

The following works just fine, but I'm not needing to create a simple DOM node, I need to use a Component I already have built and append that 以下工作正常,但是我不需要创建一个简单的DOM节点,我需要使用已经构建的组件并将其附加

var basicElement = document.createElement('h1')
basicElement.innerHTML = "This works, but I need a component to work too"
this.steps.appendChild( basicElement )

I get the following error when I try to this.steps.appendChild( component ) : 当我尝试this.steps.appendChild( component )时,出现以下错误:

Error: 错误:

Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.

I guess my main question is this: how can I convert my React Component into a node that can be used with this.steps.appendChild() ? 我想我的主要问题是:我如何将我的React组件转换成可以与this.steps.appendChild()一起使用的节点?

OR: is there a "React way" to dynamically append children components to my this.steps ? 或:是否有一种“反应方式”将子组件动态附加到我的this.steps

this.steps should be an array, then, you will be able to render that array using the map function. this.steps应该是一个数组,然后,您将能够使用map函数渲染该数组。

Also you should store your array in your state in order to auto re-render the component after you add new steps. 另外,您还应该将阵列存储在您的状态中,以便在添加新步骤后自动重新呈现组件。

It should look like this 它应该看起来像这样

 constructor( props ) {
    this.state = {
       steps: []
     }
    this.components = {
        ComponentOne: <ComponentOne />,
        ComponentTwo: <ComponentTwo />
    }
}
_appendStep( step ) {
        let componentToAdd= this.components[step];
        this.setState({steps: this.state.steps.concat([componentToAdd])})
    }

render(){
     ....
    {this.state.steps.map(function(comp,i){
        return <div key={'Step' + i}>{comp}</div>
    })}

}

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

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