代码之家  ›  专栏  ›  技术社区  ›  CNK2343

React Native-TextInput中的Firebase数据

  •  0
  • CNK2343  · 技术社区  · 8 年前

    我可以从Firebase中检索数据,然后立即显示。但当我尝试在渲染中使用它时,它是空白的。我是新手,可能做了一些不正确的事情。如何在渲染中使用Firebase中的数据?

    这是我的代码:

        componentWillMount() {
        firebase.database().ref('/users/' +       userId).once('value').then(function(snapshot) {
                                                                   var       DisplayEmail = snapshot.val().Email;                                                                       alert(DisplayEmail);
                                                                   });
    

     render() { 
            return (
    
               {alert(this.props.DisplayEmail)}
    
              <TextInput style={styles.input}
                autoFocus = {true}
                autoCorrect = {false}
                autoCapitalize = "none"
                placeholder={this.DisplayEmail}
                keyboardType="email-address"
                underlineColorAndroid='transparent'
                editable={false}
                />
    
        )
     }
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   Ramzi C.    8 年前

    无论何时,只要您想在渲染时显示某些内容,就应该将其置于state状态。通过更改状态,您将导致页面重新呈现并用新状态更新。因此,当您调用firebase时,请将其添加到状态。

    componentWillMount() {
      firebase.database().ref('/users/' + userId).on('value', snapshot => {
        this.setState({ DisplayEmail: snapshot.val().Email });
      });
    }
    

    render() { 
      return (
        {alert(this.props.DisplayEmail)}
        <TextInput style={styles.input}
          autoFocus={true}
          autoCorrect={false}
          autoCapitalize="none"
          placeholder={this.state.DisplayEmail}
          keyboardType="email-address"
          underlineColorAndroid='transparent'
          editable={false}
        />
      )
    }
    
        2
  •  0
  •   Burhan Yılmaz    8 年前

    它必须是工作:)

         componentWillMount() {
         firebase.database().ref('/users/' + userId)
                   .on('value', snapshot => {
                           this.state = { DisplayEmail: snapshot.val().Email
             });
    
    }
    

     render() { 
        return (
    
           {alert(this.props.DisplayEmail)}
    
          <TextInput style={styles.input}
            autoFocus = {true}
            autoCorrect = {false}
            autoCapitalize = "none"
            placeholder={this.state.DisplayEmail}
            keyboardType="email-address"
            underlineColorAndroid='transparent'
            editable={false}
            />
    
    )
    

    }