如果有人有更好的选择,请告诉我,但我已经这样解决了。
我有一个 ScreenComponent,它实际上丰富了我的所有子组件并为其添加了功能,例如分析和其他一些帮助程序。
基本上,我在每个屏幕上添加的每个组件都添加一个引用,然后我像这样使用 passProps:
screenParams: {
screen: 'AudioScreen',
passProps: {
componentProps: {
audioCard: {
title:'Audio Title',
subtitle:'Audio Subtitle',
image:{imageSource},
audioSource:{audioSource}
}
}
}
}
componentProps 是我使用的对象,在我的屏幕中我在 componentWillMount 中执行此操作:
this._children = React.Children.map(this.props.children, child => {
let extraProps = {
eventEmitter: this._eventEmitter
};
if (child.ref != null && self.props.componentProps && self.props.componentProps[child.ref]){
extraProps = self.getHelper().concat(extraProps, self.props.componentProps[child.ref])
}
return React.cloneElement(child, extraProps);
});
如您所见,我为每个孩子添加了一个 eventEmitter,因此我可以与我的父屏幕进行通信以显示模式、错误警报等。助手是我拥有的一个模块,它只是合并两个对象。
这样,我可以将道具发送到屏幕,将道具发送到每个组件,还可以覆盖后端的所有内容。
到目前为止,它一直运行良好。
希望对你有帮助