【问题标题】:Error on Render function渲染功能错误
【发布时间】:2018-11-24 02:21:46
【问题描述】:

我收到一个错误“渲染没有返回任何内容。这通常意味着缺少返回语句。或者,不渲染任何内容,返回 null。” 我想这与 My TimelineCard 中的类型有关。我认为类型未设置或可能未设置,但我无法弄清楚确切的位置。 在 ios 上我没有遇到同样的错误,仅在 android 上。我正在附加我的 Render 函数。

我的时间线卡片:

renderInfoBar(width, item) {
        let dotSize = 6;

    if (Platform.OS === "ios") {
        return (
            <View style={{width: width - 80, flexDirection: 'row', backgroundColor: Colors.white}}>
                <View style={{flex: 1, flexDirection: 'row',  alignItems: 'center', justifyContent: 'flex-start'}}>
                    <Text style={{marginRight: 0, marginLeft: 10, fontSize: 12, fontWeight: 'bold', color: Colors.darkText}}>{numeral(item.likes).format('0,0')} likes</Text>
                    <View style={{width: dotSize, height: dotSize, backgroundColor: Colors.darkText, borderRadius: 25, marginLeft: 10, marginRight: 10}}></View>
                    <Text style={{fontSize: 12, fontWeight: 'bold', color: Colors.darkText}}>{numeral(item.shares.length).format('0,0')} shares</Text>
                </View>
                <View style={{flex: 1, alignItems: 'center', flexDirection: 'row', justifyContent: 'flex-end', marginTop: 0}}>
                    <Text style={{fontSize: 12, fontWeight: 'bold', color: Colors.darkText}}>{numeral(item.views).format('0,0')}</Text>
                    <Image source={require("../assets/images/views-icon.png")} style={{marginRight: 10, marginLeft: 5}}/>
                </View>
            </View>                                                         
        );  
    }
    else {
        return (
            <View style={{width: width - 80, height: 30, flexDirection: 'row', backgroundColor: Colors.white}}>
                <View style={{flex: 1, flexDirection: 'row', justifyContent: 'flex-start', marginTop: 2}}>
                    <Text style={{marginRight: 0, marginLeft: 10, fontSize: 12, fontWeight: 'bold', color: Colors.darkText}}>{numeral(item.likes).format('0,0')} likes</Text>
                    <Entypo name="dot-single" style={{marginTop: -7}} size={32} color={Colors.darkText} />                          
                    <Text style={{fontSize: 12, fontWeight: 'bold', color: Colors.darkText}}>{numeral(item.shares.length).format('0,0')} shares</Text>
                </View>
                <View style={{flex: 1, alignItems: 'center', flexDirection: 'row', justifyContent: 'flex-end', marginTop: 0, marginBottom: 7}}>
                    <Text style={{fontSize: 12, fontWeight: 'bold', color: Colors.darkText}}>{numeral(item.views).format('0,0')}</Text>
                    <Image source={require("../assets/images/views-icon.png")} style={{marginRight: 10, marginLeft: 5}}/>
                </View>
            </View>                                                         
        );  
    }
}

render() {

let {width, height} = Dimensions.get('window');

let containerStyle = {flex: 1, marginLeft: 10, marginBottom: 20};

if (this.state.rowData.type === PERMISSION_REQUEST) {
    return (
        <View style={containerStyle}>
            {this.renderNotification(this.state.rowData)}
        </View>
    )           
}
else if (this.state.rowData.type === PERMISSION_GRANTED) {
    return (
        <View style={containerStyle}>
            {this.renderNotification(this.state.rowData)}
        </View>
    )           
}
else if (this.state.rowData.type === PERMISSION_DECLINED) {
    return (
        <View style={containerStyle}>
            {this.renderNotification(this.state.rowData)}
        </View>
    )           
}
else if (this.state.rowData.type === PERMISSION_CANCELED) {
    return (
        <View style={containerStyle}>
            {this.renderNotification(this.state.rowData)}
        </View>
    )           
}
else if (this.state.rowData.type === PHOTO_VIEWED) {
    return (
        <View style={containerStyle}>
            {this.renderNotification(this.state.rowData)}
        </View>
    )           
}
else if (this.state.rowData.type === PHOTO_LIKED) {
    return (
        <View style={containerStyle}>
            {this.renderNotification(this.state.rowData)}
        </View>
    )           
}
else if (this.state.rowData.type === PHOTO_UNLIKED) {
    return (
        <View style={containerStyle}>
            {this.renderNotification(this.state.rowData)}
        </View>
    )           
}
else if (this.state.rowData.type === PHOTO_SHARED) {
    return (
        <View style={containerStyle}>
            {this.renderNotificationWithFullPhoto(this.state.rowData)}
        </View>
    )           
}
else if (this.state.rowData.type === PHOTO_COMMENTED) {
    return (
        <View style={containerStyle}>
            {this.renderNotification(this.state.rowData)}
        </View>
    )   
}
else if (this.state.rowData.type === PHOTO_TAGGED) {
    return (
        <View style={containerStyle}>
            {this.renderNotification(this.state.rowData)}
        </View>
    )           
}
else if (this.state.rowData.type === PHOTO_NOTED) {
    return (
        <View style={containerStyle}>
            {this.renderNotification(this.state.rowData)}
        </View>
    )           
}
else if (this.state.rowData.type === PHOTO_CREATED) {
    return (
        <View style={containerStyle}>
            {this.renderNotificationWithFullPhoto(this.state.rowData)}
        </View>
    )                                 
}
else if (this.state.rowData.type === PHOTO_SCREENSHOT_CAPTURE) {
    return (
        <View style={containerStyle}>
            {this.renderNotification(this.state.rowData)}
        </View>
    )           
}
else if (this.state.rowData.type === PHOTO_UNSHARED) {
    return (
        <View style={containerStyle}>
            {this.renderNotification(this.state.rowData)}
        </View>
    )           
}

}

【问题讨论】:

  • 就像@Khoa 说的那样。您需要添加 else 以便查看缺少的内容:else {console.log(this.state.rowData.type);}else {return (&lt;View&gt;&lt;Text&gt;{this.state.rowData.type}&lt;/Text&gt;&lt;/View&gt;})}(在屏幕上打印)
  • @gaback 您的建议适用于 else {return ({this.state.rowData.type}})} 。当我问 Khoa 时,我想得到一些更进一步的解释,所以我会更好地理解它,因为我没有编写代码。

标签: android reactjs react-native render react-proptypes


【解决方案1】:

建议很明确:

或者,不渲染,返回 null。

你需要用return null;添加最后一个else(不是else if

您可能缺少 1 个案例 => 在 return null 之前添加一个 console.log 以检查它是什么。

【讨论】:

  • 嗨,Khoa,感谢您的建议,我添加了 else{return(null);} ,它可以工作。您介意进一步解释背后的逻辑吗,我没有编写代码,因此我不太熟悉它的工作原理。谢谢
  • @IlonaSemyonov:render 函数应返回Componentnull。对于ifelse if 的每种情况,旧代码返回Component。如果rowDatatype 与上述所有情况都不匹配怎么办=> 没有任何回报。为了防止这种情况,我们再添加 1 个else 子句来处理所有意外情况。 else 子句将捕获所有其余情况。
猜你喜欢
  • 2020-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-03
相关资源
最近更新 更多