Reactjs 将子组件用作真实组件,而不是无状态子对象

Reactjs 将子组件用作真实组件,而不是无状态子对象,reactjs,Reactjs,某些组件包含带有选项卡的选项卡: class App { render() { <div> <p>Tabs here:</p> <Tabs> <Tab name="Page 1"> .. content here .. </Tab> <Tab name="Page 2"> .. content here .. </Tab>

某些组件包含带有
选项卡的
选项卡

class App {
  render() {
    <div>
      <p>Tabs here:</p>
      <Tabs>
        <Tab name="Page 1"> .. content here .. </Tab>
        <Tab name="Page 2"> .. content here .. </Tab>
        <Tab name="Page 3"> .. content here .. </Tab>
      </Tabs>
    </div>
  }
}
选项卡本身很小,可能什么都没有:

const Tab = ({children}) => <div>{ children }</div>;

但是
Tabs
不能这样做,因为它没有
子对象的
Tab
对象。。。为什么?

好吧,有几种方法可以实现这一点。首先,您是对的-因为您在
映射中使用ES6 arrow函数,
这个
不是反弹-它是从封闭范围中“继承”的(我认为,从技术上讲,它继承较少,更简单-没有改变)

首先,不要在render方法中绑定,而是在构造函数中单击bind

constructor(props) {
    super(props);

    this.click = this.click.bind(this);
}
或者使用ES6箭头函数

click = (event) => {...}
尽管这并不能直接解决您的问题,但它会清除因使用绑定回调而产生的上下文混乱

从那里,您可以使用
event.target
获得单击的选项卡

您还可以使用partials-bind方法接受附加参数,这些参数是绑定函数参数的前缀。看起来是这样的:

render (
    <div>
    { this.props.children.map((tab) => {
        <a <li><a onClick={ this.click.bind(null, tab) } href="#">{ tab.props.name }</a></li>
    }) }
);
编辑:工作示例

选项卡

class Tabs extends React.Component {
    onTabClick = (tab) => {
        tab.setState({ test: true });
    }

    render() {
        return (
            <div>
                { this.props.children.map((tab) => {
                    return React.cloneElement(tab, {
                        onClick: this.onTabClick
                    });
                }) }
            </div>
        );
    }
}
class Tab extends React.Component {
    constructor(props) {
        super(props);
        this.state = {};
    }

    onClick = (event) => {
        console.log(event);
        event.preventDefault();

        if (this.props.onClick) {
            this.props.onClick(this);
        }
    }
    render() {
        return <div>{ !this.state.test && <a href={ this.props.url } onClick={ this.onClick }>{ this.props.label }</a> }</div>;
    }
}
示例选项卡

<Tabs>
    <Tab url="http://www.google.com" label="Google" />
    <Tab url="http://www.google.com" label="Google" />
</Tabs>


尽管它再次回避了这个问题——为什么不在
选项卡中设置状态呢?为什么需要从父对象中设置子对象的状态

最简单的方法是允许
在调用其
onClick
函数时获取标识值并将其传递回链。例如:您可以将
this.props.name
作为
onClick
@MikeDriver上的第二个参数传递,但我仍然没有
选项卡
对象。。。为什么props.children没有
选项卡
对象?我想在
选项卡上
设置state()
,或者读取其状态,或者执行一些
选项卡
逻辑。@MikeDriver我在问题中添加了edit 1,说明我为什么想要真正的选项卡对象。
event.target
将是DOM元素。不是
选项卡
对象。绑定不是问题所在<代码>道具。儿童
甚至没有
选项卡
对象。如果执行
React.cloneElement
,它会引用原始
选项卡
对象吗??因为您克隆的
选项卡
不是
选项卡
对象,这就是问题所在。选项卡中的共享状态可能更好,但我在Vue示例中看到了这一点,并希望在React=)中实现它
选项卡
具有双重显示(LIs和内容),因此仅呈现
选项卡
是不够的<代码>选项卡
必须渲染每个
选项卡
两次,但我不是一个大风扇…我已经添加了编辑1的问题,为什么我喜欢真正的标签对象。
class Tabs extends React.Component {
    onTabClick = (tab) => {
        tab.setState({ test: true });
    }

    render() {
        return (
            <div>
                { this.props.children.map((tab) => {
                    return React.cloneElement(tab, {
                        onClick: this.onTabClick
                    });
                }) }
            </div>
        );
    }
}
class Tab extends React.Component {
    constructor(props) {
        super(props);
        this.state = {};
    }

    onClick = (event) => {
        console.log(event);
        event.preventDefault();

        if (this.props.onClick) {
            this.props.onClick(this);
        }
    }
    render() {
        return <div>{ !this.state.test && <a href={ this.props.url } onClick={ this.onClick }>{ this.props.label }</a> }</div>;
    }
}
<Tabs>
    <Tab url="http://www.google.com" label="Google" />
    <Tab url="http://www.google.com" label="Google" />
</Tabs>