如何让子组件的子组件引用第一级子组件可以使用的组件?

时间:2016-11-22 21:34:30

标签: javascript reactjs react-jsx jsx

如果我有以下JSX,则<dependency> <groupId>org.sql2o</groupId> <artifactId>sql2o</artifactId> <version>1.5.4</version> </dependency> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.15.1</version> </dependency> &#39; s Exception in thread "main" java.lang.RuntimeException: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at org.sql2o.JndiDataSource.getJndiDatasource(JndiDataSource.java:27) at org.sql2o.Sql2o.<init>(Sql2o.java:36) at me.mypackage.sqlitetest.SqliteTest.main(SqliteTest.java:11) Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(Unknown Source) at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source) at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source) at javax.naming.InitialContext.lookup(Unknown Source) at org.sql2o.JndiDataSource.getJndiDatasource(JndiDataSource.java:24) ... 2 more 为空:

Child

我希望refs组件在其<Parent> <Child ref="first"> <GrandChild ref="parentSeesThisNotChild" /> </Child> </Parent> 中获取Child。我该怎么做?

1 个答案:

答案 0 :(得分:0)

您必须修改Child组件代码:

const Child = React.createClass({
  render() {
    const childrenWithProps = React.Children.map(this.props.children, (child) => {
      return React.cloneElement(child, {
        ref: child.ref,
      });
    });

    return (
      <div>
        <h1>Child</h1>
        {childrenWithProps}
      </div>
    )
  },
});