简体   繁体   English

遍历 Firebase 数据库 object 并分配键以反应组件

[英]Iterating through Firebase database object and assining keys to react components

I am working in ReactJS and I have a database on Firebase called posts as a collection of objects.我在 ReactJS 工作,我在 Firebase 上有一个数据库,称为帖子作为对象的集合。 I am trying to iterate through the database objects and return a component with each assigned with one of the unique keys that firebase creates for the objects as props.我正在尝试遍历数据库对象并返回一个组件,每个组件都分配有 firebase 为对象创建的唯一键之一作为道具。

As a result example of what I am trying to achieve:作为我试图实现的结果示例:

在此处输入图像描述

<Post
   key={-MQcz3BC4lbKnvFe8Jl}
   title={post.title}
   type={post.type}
   body={post.body}
   clicked={() => this.postAnswerHandler( post.id )}
    />

<Post
   key={-MQxVra23HwWb8ogRJZ}
   title={post.title}
   type={post.type}
   body={post.body}
   clicked={() => this.postAnswerHandler( post.id )}
    />

...and so on. ...等等。 Can anyone help with iterating through the firebase Data and assigning the keys to my React components?任何人都可以帮助遍历 firebase 数据并将密钥分配给我的 React 组件吗?

Here is the current code I am using for this:这是我为此使用的当前代码:

class Posts extends Component {
    state = {
        posts: []
    }

    componentDidMount () {
   
        axios.get( 'https://blog-6d4da-default-rtdb.firebaseio.com/posts.json' )
            .then( response => {
                let posts = Object.values(response.data);
                let key = Object.keys(response.data);
            
                const updatedPosts = posts.map( post => {
                    
                    return {
                        ...post,
                        
                    }
                
                } );

                this.setState( { posts: updatedPosts } );
                
            } )
            .catch( error => {
                console.log( error );
                // this.setState({error: true});
            } );
    }

  

    render () {
        let posts = <p style={{ textAlign: 'center' }}>Something went wrong!</p>;
        if ( !this.state.error ) {
            posts = this.state.posts.map( (post) => {
                return (
                    
                    <Post
                        key={post.key}
                        title={post.title}
                        type={post.type}
                        body={post.body}
                        clicked={() => this.postAnswerHandler( post.id )}
                         />
                        
                    // </Link>
                );
            } );
        }

I think you're looking for:我想你正在寻找:

let keys = Object.keys(response.data);

const updatedPosts = keys.map( key => {    
    return {
        key, ...response.data[key],
        
    }
} );

this.setState( { posts: updatedPosts } );

Use Object.entries()使用Object.entries()

 axios.get( 'https://blog-6d4da-default-rtdb.firebaseio.com/posts.json' )
            .then( response => {
            
               this.setState( { posts: response } );
                
            } )

render () {
        let posts = <p style={{ textAlign: 'center' }}>Something went wrong!</p>;
        if ( !this.state.error ) {
            posts = Object.entries(this.state.posts).map( ([post, key]) => {
                return (
                    
                    <Post
                        key={key}
                        title={post.title}
                        type={post.type}
                        body={post.body}
                        clicked={() => this.postAnswerHandler( post.id )}
                         />
                        
                    // </Link>
                );
            } );
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM