假设我有以下反应代码。React组件如何触发兄弟React组件的功能
<Navigation parentWindow={this} />
<p>Sub Pages</p>
<ReactCSSTransitionGroup
component="div"
transitionName="page-transition"
transitionEnterTimeout={0}
transitionLeaveTimeout={500}
>
{React.cloneElement(this.props.children, {
key: location.pathname
})}
</ReactCSSTransitionGroup>
的ReactCSSTransitionGroup
将最终渲染的是由ContactPage.js
创建一个<ContactPage />
。下面是ContactPage.js
样子:
import React from 'react';
export default class Page extends React.Component
{
testMe() {alert('Hello World!');}
render() {return <div>Hello</div>;}
}
从我<Navigation />
这是由Navigation.js
创建的,我希望能够触发ContactPage.testMe()
。所以,我做这在我的Navigation.js
import React from 'react';
export default class Page extends React.Component
{
render() {
this.props.parentWindow.props.children.testMe();
return <div>Navigate me</div>;
}
}
但是当我运行该项目,我的导航给我的错误:
Uncaught TypeError: this.props.parentWindow.props.children.testCall
我该如何解决这个问题得到什么?
你有导出默认的类页面扩展你的Navigation.js中的React.Component。这只是一个复制和粘贴错误? – user5325596