简体   繁体   English

ReactJS操纵现有的DOM元素

[英]ReactJS manipulate existing DOM element

I got this task during a job interview. 我在求职面试中完成了这项任务。 I'm new to ReactJS so I couldn't find a proper solution. 我是ReactJS的新手,所以我找不到合适的解决方案。

I'm supposed to make something like bootstrap's toggle box: image example 我应该做一些像bootstrap的切换框: 图像示例

The important thing here is that the component should be invoked this way: 这里重要的是应该以这种方式调用组件:

<ToggleBox header="The title goes here">
   The content of the toggle box
</ToggleBox>

I'm not sure but I think they mean that the invoking HTML code is attached to the DOM and not using React.render(). 我不确定,但我认为他们的意思是调用的HTML代码附加到DOM而不是使用React.render()。

However, as far as I know, ReactJS isn't structured for rebuilding existing DOM, is there any workaround to accomplish this task? 但是,据我所知,ReactJS不是用于重建现有DOM的结构,是否有任何解决方法来完成此任务? Maybe I got it all wrong and by "invoking" they mean to use React.render()? 也许我弄错了,“调用”他们的意思是使用React.render()?

Thanks! 谢谢! :-) :-)

The component would need to use this.props.children . 该组件需要使用this.props.children You can look at the documentation here . 你可以在这里查看文档

Here's an example: 这是一个例子:

var WithChildren = React.createClass({
    render: function() {
        var child = this.props.children;
        var header = this.props.header;
        return (
            <div>
                <div>{header}</div>
                <div>{child}</div>
            </div>
        );
    }
});

React.render(<WithChildren header="Title goes here">Some content</WithChildren>, document.querySelector('.app'));

If you run that you will see Some content rendered. 如果你运行它,你会看到Some content呈现。 So all you would need is the extra markup and CSS to make it look like the image. 所以你需要的是额外的标记和CSS,使它看起来像图像。

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

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