how do i change firebase output to text with native react from what i have now

非 Y 不嫁゛ 提交于 2020-01-25 08:33:04

问题


I want to get the output to say a just 'hi' instead of -LrM8rlKq1-dSW6XRt0_({"color: 'blue"}) on any of the outputs(update, delete, create ), and how do I hide seletedid(-LrM8rlKq1-dSW6XRt0_) from showing up on the display

/////////////////////////////////////////////////////////////////////////////////////////////////////////

 import React, { Component } from 'react';
    import { View, Text, StyleSheet, Button, TextInput, TouchableOpacity } from 'react-native';
    import firebase from './firebase';
    export default class App extends Component {
    carDatabase = firebase.database().ref('car');
     state = { cars: {}, selectedId: '' }
     // Read
     componentDidMount() {
       this.carDatabase.on('value', cars => {
         const carsJSON = cars.val();
         this.setState({ cars: carsJSON === null ? {} : carsJSON });
       })
       // this.carDatabase.push({color: 'yellow'})
     }
     // Create
     create() {
       this.carDatabase.push({color: 'yellow'})  
       this.setState({selectedId: ''})
     }
     // Update
     update() {
      this.carDatabase.child(this.state.selectedId).set({color: 'blue'}) 
      this.setState({selectedId: ''})
    }
     // Delete
     deleteCar() {
        if(this.state.selectedId === '') {
          return;

        }

       this.carDatabase.child(this.state.selectedId).set(null)
      this.setState({selectedId: ''})
    }
        render() {
            return (
                <View style={styles.container}>
                 <TextInput value={this.state.selectedId} style={styles.textInput}></TextInput>
                   <Button title="create" onPress={() => this.create()}></Button> 
                   <Button title="update" onPress={() => this.update()}></Button> 
                   <Button title="delete" onPress={() => this.deleteCar()}></Button> 
           {
             Object.keys(this.state.cars).map( (carId, index) =>
               <TouchableOpacity key={index} onPress={() => this.setState({ selectedId: carId})}>
                 <Text>{`${carId}: ${JSON.stringify(this.state.cars[carId])}`}</Text>
               </TouchableOpacity>
             )
           }

           {/* <Text>{JSON.stringify(this.state.cars, null, 2)}</Text> */}
         </View>
            );
        }
    }
    const styles = StyleSheet.create({
     textInput: {
       backgroundColor: 'green',
       height: 30,
       width: '100%'
     },
        container: {
            flex: 1,
            backgroundColor: '#fff',
            alignItems: 'center',
            justifyContent: 'center'
     }
    });


  [1]: https://i.stack.imgur.com/tQHSK.jpg

回答1:


I don't really understand your problem here: Just remove this part of your code

{
   Object.keys(this.state.cars).map( (carId, index) => (
      <TouchableOpacity key={index} onPress={() => this.setState({ selectedId: carId})}>
         <Text>{`${carId}: ${JSON.stringify(this.state.cars[carId])}`}</Text>
      </TouchableOpacity>
   )
}

And replace it by this :

{
   Object.keys(this.state.cars).map( (carId, index) => (
         <Text style={{color: this.state.cars[carId].color}}>hi !</Text>
   )
}


来源:https://stackoverflow.com/questions/58507653/how-do-i-change-firebase-output-to-text-with-native-react-from-what-i-have-now

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!