javascript React.js:组合组件以创建选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20872632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:30:50  来源:igfitidea点击:

React.js: Composing components to create tabs

javascriptcomponentscompositionreactjs

提问by NVI

I'm trying to make a tabs component. TabsSwitcher and TabsPanel must be separate components so they could be used anywhere in DOM, e.g. TabsSwitcher doesn't have to be followed by TabsPanel.

我正在尝试制作标签组件。TabsSwitcher 和 TabsPanel 必须是独立的组件,以便它们可以在 DOM 中的任何地方使用,例如 TabsSwitcher 不必跟在 TabsPanel 之后。

To make it work, I need to somehow connect these components. Furthermore, TabsSwitcher must be able to tell TabsPanel when a tab was clicked.

为了使它工作,我需要以某种方式连接这些组件。此外,TabsSwitcher 必须能够在单击选项卡时告诉 TabsPanel。

/** @jsx React.DOM */

var TabsExample = React.createClass({
    render: function() {
        var tabs = [
            {title: 'first', content: 'Content 1'},
            {title: 'second', content: 'Content 2'}
        ];
        return <div>
            <TabsSwitcher items={tabs}/>
            <TabsContent items={tabs}/>
        </div>;
    }
});

var TabsSwitcher = React.createClass({
    render: function() {
        var items = this.props.items.map(function(item) {
            return <a onClick={this.onClick}>{item.title}</a>;
        }.bind(this));
        return <div>{items}</div>;
    },
    onClick: function(e) {
        // notify TabsContent about the click
    }
});

var TabsContent = React.createClass({
    render: function() {
        var items = this.props.items.map(function(item) {
            return <div>{item.content}</div>;
        });
        return <div>{items}</div>;
    }
});

React.renderComponent(
    <TabsExample/>,
    document.body
);

What's the best way to do it?

最好的方法是什么?



Solution: http://jsfiddle.net/NV/5YRG9/

解决方案:http: //jsfiddle.net/NV/5YRG9/

回答by Ross Allen

The React docs cover this in detail in "Communicate Between Components" and "Multiple Components". The gist is that the parent should pass a function as a prop to the child, and the child should call that function as a callback when it needs to:

React 文档在“组件间通信”和“多个组件”中详细介绍了这一点。要点是父母应该将一个函数作为道具传递给孩子,而孩子应该在需要时将该函数作为回调调用:

var TabsExample = React.createClass({
    handleTabClick: function(item) {
        // Do something with item, maybe set it as active.
    },
    render: function() {
        var tabs = [
            {title: 'first', content: 'Content 1'},
            {title: 'second', content: 'Content 2'}
        ];
        return <div>
            <TabsSwitcher items={tabs} onTabClick={this.handleTabClick}/>
            <TabsContent items={tabs}/>
        </div>;
    }
});

var TabsSwitcher = React.createClass({
    render: function() {
        var items = this.props.items.map(function(item) {
            return <a onClick={this.onClick.bind(this, item)}>{item.title}</a>;
        }.bind(this));
        return <div>{items}</div>;
    },
    onClick: function(item) {
        this.props.onTabClick(item);
    }
});

For the TabsContentcomponent, you should move the tabsinto the TabsExamplestate so React can automatically re-render for you when they change. Since TabsSwitcherand TabsContentare passed the tabs in the render method, React knows they are dependent on the tabs and will re-render when the state changes:

对于TabsContent组件,您应该将其移动tabsTabsExamplestate 中,以便 React 可以在它们更改时自动为您重新渲染。由于TabsSwitcherTabsContent在渲染方法中传递了选项卡,React 知道它们依赖于选项卡,并且会在状态更改时重新渲染:

var TabsExample = React.createClass({
    getInitialState: function() {
        return {
            activeTabId: 1,
            tabs: [
                {title: 'first', content: 'Content 1', id: 1},
                {title: 'second', content: 'Content 2', id: 2}
            ]
        };
    };
    handleTabClick: function(item) {
        // Call `setState` so React knows about the updated tab item.
        this.setState({activeTabId: item.id});
    },
    render: function() {
        return (
            <div>
                <TabsSwitcher items={this.state.tabs}
                              activeItemId={this.state.activeTabId}
                              onTabClick={this.handleTabClick}/>
                <TabsContent items={this.state.tabs}
                             activeItemId={this.state.activeTabId}/>
            </div>
        );
    }
});