【问题标题】:React-Native Modal does not show when Dom rerender当 Dom 重新渲染时,React-Native Modal 不显示
【发布时间】:2017-07-07 04:21:01
【问题描述】:

在我的渲染函数中,我提出了一个请求,所以加载时会显示“正在加载”。收到数据后,我调用“setState”来重新渲染DOM。我想显示模态,但模态不存在。我怎么了?

 render: function() {

    if (!this.props.data) {
           return (<View style={{flex:1}}>
                    <Text>loading</Text>
                </View>);
    }
    return (
        <View style={{flex:1, width:screenWidth}}>
         <Modal
                visible={true}
                transparent={true}
                onRequestClose={()=>{
                }}
                onShow={()=>{
                }}>
                <View
                    style={{flex:1,justifyContent:'center',alignItems:'center',backgroundColor:'rgba(0, 0, 0, 0.3)'}}>
                    <View style={{height:200,width:275,backgroundColor:'white'}}>
                        <Button title='confirm' onPress={()=>{}}/>
                        <Button title='cancel' onPress={()=>{}}/>
                    </View>
                </View>
            </Modal>
        </View>
    );
}

【问题讨论】:

    标签: reactjs react-native modal-dialog


    【解决方案1】:

    您可以在单个返回中检查条件,例如:

    render(){
        return(
            {(!this.props.data) 
            ? (<View style={{flex:1}}>
                  <Text>loading</Text>
              </View>)
            : (<View style={{flex:1, width:screenWidth}}>
                <Modal/> { /*your modal code */}
            </View>)
            }
        );
    }
    

    首先检查您的模态是否无条件打开,如果是,则它是样式或重新渲染问题。

    【讨论】:

    • @Eric 你有问题吗?
    • 我收到一个错误,“意外令牌”。也许'?:'不能在返回函数中使用?
    • 你可以使用三元条件作为回报,我几乎每个页面都使用过
    • 无法加载源:错误域=JSServer Code=500 "SyntaxError XXX.js: Unexpected token (583:13)" ,代码 "({!this.props.data)" 在行583.跨度>
    • 我使用了三元条件。
    猜你喜欢
    • 2019-09-26
    • 2018-12-27
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-12
    • 2017-04-20
    相关资源
    最近更新 更多