如何将反应组件拆分为嵌套组件

时间:2017-02-01 09:15:27

标签: javascript reactjs

我想拆分一个react组件,因为我想为meta - 块创建一个自己的组件。所以这是我目前的组成部分:

/imports/ui/components/example.jsx

import React, { Component } from 'react';

export default class Example extends Component {
    constructor(props) {
        super(props);
        this.state = { activeMetaTab: 'First' };
        this.setActiveClassOnMetaMenu = this.setActiveClassOnMetaMenu.bind(this);
        this.handleChangeTabs = this.handleChangeTabs.bind(this);
    }

    setActiveClassOnMetaMenu(tab) {
        return this.state.activeMetaTab === tab ? 'active' : '';
    }

    handleChangeTabs(event) {
        this.setState({ activeMetaTab: event.target.textContent });
    }

    render() {
        return (
            <div className="ui raised padded text container segment">
                <h1 className="ui right floated header">Otitis media</h1>
                <div className="ui clearing divider"></div>

                <div id="meta" className="ui segment">
                    <div className="ui compact mini menu secondary vertical pointing">
                        { this.props.meta.map((item) => {
                            return (<a className={`item ${ this.setActiveClassOnMetaMenu(item.title) }`} onClick={ this.handleChangeTabs } key={ item.title}>{ item.title }</a>);
                        }) }
                    </div>
                    { this.props.meta.map((item, index) => {
                        return (
                            <div className={`ui tab content ${ this.setActiveClassOnMetaMenu(item.title) }`} key={ index }>
                                <p>{ item.content }</p>
                            </div>
                        );
                    }) }
                </div>

            </div>
        );
    }
}

/client/main.js

import Example from '../imports/ui/components/example.jsx';

render(<Example
    meta = {[
        { title: 'First', 'content': 'Lorem ipsum 1' },
        { title: 'Second', 'content': 'Lorem ipsum 2' },
        { title: 'Third', 'content': 'Lorem ipsum 3' },
    ]}
/>, document.getElementById('react-root'));

我想使用meta组件,但我不知道如何将组件嵌套到另一个(主要)组件中。所以我想出了这个:

/imports/ui/components/example.jsx

import React, { Component } from 'react';

import ExampleMeta from './example-meta.jsx';

export default class Example extends Component {
    render() {
        return (
            <div className="ui raised padded text container segment">
                <h1 className="ui right floated header">Otitis media</h1>
                <div className="ui clearing divider"></div>

                <ExampleMeta />
            </div>
        );
    }
}

/imports/ui/components/example-meta.jsx

import React, { Component } from 'react';

export default class ExampleMeta extends Component {
    constructor(props) {
        super(props);
        this.state = { activeMetaTab: 'First' };
        this.setActiveClassOnMetaMenu = this.setActiveClassOnMetaMenu.bind(this);
        this.handleChangeTabs = this.handleChangeTabs.bind(this);
    }

    setActiveClassOnMetaMenu(tab) {
        return this.state.activeMetaTab === tab ? 'active' : '';
    }

    setActiveClassOnContentMenu(item) {
        return this.state.activeContentTab === item ? 'active' : '';
    }

    handleChangeTabs(event) {
        this.setState({ activeMetaTab: event.target.textContent });
    }

    render() {
        return (
            <div id="meta" className="ui segment">
                <div className="ui compact mini menu secondary vertical pointing">
                    { this.props.meta.map((item) => {
                        return (<a className={`item ${ this.setActiveClassOnMetaMenu(item.title) }`} onClick={ this.handleChangeTabs } key={ item.title}>{ item.title }</a>);
                    }) }
                </div>
                { this.props.meta.map((item, index) => {
                    return (
                        <div className={`ui tab content ${ this.setActiveClassOnMetaMenu(item.title) }`} key={ index }>
                            <p>{ item.content }</p>
                        </div>
                    );
                }) }
            </div>
        );
    }
}

1 个答案:

答案 0 :(得分:0)

你所做的几乎是正确的。但是,您必须转发 meta道具到<ExampleMeta />

/imports/ui/components/example.jsx

import React, { Component } from 'react';

import ExampleMeta from './example-meta.jsx';

export default class Example extends Component {

    render() {
        return (
            <div className="ui raised padded text container segment">
                <h1 className="ui right floated header">Otitis media</h1>
                <div className="ui clearing divider"></div>

                <ExampleMeta meta={this.props.meta} />
            </div>
        );
    }
}