【问题标题】:React flow error inner render method反应流错误内部渲染方法
【发布时间】:2017-08-30 12:41:55
【问题描述】:

EDIT: 我添加了React.Element<*>作为返回类型,并返回<View />而不是null,但仍然有同样的错误。

而我的流程版本是0.53.1

render() {
   return (
     <View style={{ flex: 1 }} >
       <TabHeader title={strings.tabBarLabelRecommend} />
       {this._renderVideoCategory()}
     </View>
   );
}

上面是我的渲染方法,_renderVideoCategory 是:

_renderVideoCategory = (): React.Element<*> => {
  if (!this.props.recommendVideos.length) {
    return <View />;
  }
   this.tabWidth = C.SCREEN_WIDTH / this.props.recommendVideos.count;
   return (
     <TabViewAnimated style={{ flex: 1 }} 
       navigationState={this.state} 
       renderScene={this._renderScene}
       renderHeader={this._renderHeader}
       renderPager={this._renderPager}
       onRequestChangeTab={this._handleChangeTab}
     />
   );
};

但是流程显示错误:(第150行是{this._renderVideoCategory()})

Error:(150, 10) Flow: exact type: object type. This type is incompatible with union: undefined | null | boolean | number | string | type application of type React$Element | type application of identifier Iterable

【问题讨论】:

  • 这与您的答案无关。现在我正在学习 React-Native,但我没有找到任何好的网站,如果你知道任何好的网站并且都在一个地方,你能分享一下吗?
  • 您能准确说明代码中第 150 行的位置吗?

标签: reactjs react-native flowtype


【解决方案1】:

尝试返回一个空视图,因为 Flow 期望您返回一个 React.Element(在示例代码中也有注释):

_renderVideoCategory = ():React.Element<*> => {
  if (!this.props.recommendVideos.length) {
    return <View />;
  }

  this.tabWidth = C.SCREEN_WIDTH / this.props.recommendVideos.count;

  return (
  <TabViewAnimated
    style={{ flex: 1 }}
    navigationState={this.state}
    renderScene={this._renderScene}
    renderHeader={this._renderHeader}
    renderPager={this._renderPager}
    onRequestChangeTab={this._handleChangeTab}
  />);
}

【讨论】:

    【解决方案2】:

    而不是返回 null 返回一个空的 div

    if (this.props.recommendVideos.length === 0) {
        return <View/>;
    }
    

    编辑

    你能改变你声明函数的方式吗

    _renderVideoCategory (): React.Element<*>{
        if (this.props.recommendVideos.length === 0) {
            return <View/>;
        }
        this.tabWidth = C.SCREEN_WIDTH / this.props.recommendVideos.count;
        return (
            <TabViewAnimated
                  style={{ flex: 1 }}
                  navigationState={this.state}
                  renderScene={this._renderScene}
                  renderHeader={this._renderHeader}
                  renderPager={this._renderPager}
                  onRequestChangeTab={this._handleChangeTab}
            />
        );
    }
    

    【讨论】:

    • @yifan_z 第150行有什么?
    • {this._renderVideoCategory()}
    • 我添加了 React.Element 作为返回类型,并返回 而不是 null,但仍然有同样的错误。
    猜你喜欢
    • 2016-10-30
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多