Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 设置状态=>;如果数组为空,则不展开-反应_Reactjs_Firebase_Redux - Fatal编程技术网

Reactjs 设置状态=>;如果数组为空,则不展开-反应

Reactjs 设置状态=>;如果数组为空,则不展开-反应,reactjs,firebase,redux,Reactjs,Firebase,Redux,这是在两个firebase集合中添加注释的代码。。。如果“this.props.movie.comments”中有一个数组中的项目,那么一切都很好。。。如果该数组为空,则问题开始出现 如果数组为空,如何跳过该扩展函数 我这样做对吗 谢谢我认为最简单的方法是空检查,因为空数组仍然可以在构造函数中或此处传播。通过将局部常量设置为数组或空数组,可以在不复制代码的情况下执行此操作 handleCommentChange = (e) => { const commentID = idGene

这是在两个firebase集合中添加注释的代码。。。如果“this.props.movie.comments”中有一个数组中的项目,那么一切都很好。。。如果该数组为空,则问题开始出现

如果数组为空,如何跳过该扩展函数

我这样做对吗


谢谢

我认为最简单的方法是空检查,因为空数组仍然可以在构造函数中或此处传播。通过将局部常量设置为数组或空数组,可以在不复制代码的情况下执行此操作

handleCommentChange = (e) => {
    const commentID = idGenerator();
    this.setState({
        movie: {
            movieId: this.props.match.params.id,
            ...this.props.movie,           
            comments:[
                ...this.props.movie.comments,
                {     
                    commentID: commentID,
                    by:this.props.profile.firstName,                   
                    userID: this.props.auth.uid,
                    createdAt: new Date(),
                    comment: e.target.value               
                }               
            ]
        },


        profile:{
            ...this.props.profile,
            comments:[
                ...this.props.profile.comments,
                {                              
                    movieId: this.props.match.params.id,
                    commentID: commentID,                                             
                    createdAt: new Date(),
                    comment: e.target.value               
                }
            ]                           

        }
    })
}

this.props.profile.comments
也可以使用同样的方法,以防您在那里有相同的行为。通常,我会确保总是传递空数组或填充数组,而不是作为道具传递null。

我认为最简单的方法是进行null检查,因为空数组仍然可以在构造函数中或此处传播。通过将局部常量设置为数组或空数组,可以在不复制代码的情况下执行此操作

handleCommentChange = (e) => {
    const commentID = idGenerator();
    this.setState({
        movie: {
            movieId: this.props.match.params.id,
            ...this.props.movie,           
            comments:[
                ...this.props.movie.comments,
                {     
                    commentID: commentID,
                    by:this.props.profile.firstName,                   
                    userID: this.props.auth.uid,
                    createdAt: new Date(),
                    comment: e.target.value               
                }               
            ]
        },


        profile:{
            ...this.props.profile,
            comments:[
                ...this.props.profile.comments,
                {                              
                    movieId: this.props.match.params.id,
                    commentID: commentID,                                             
                    createdAt: new Date(),
                    comment: e.target.value               
                }
            ]                           

        }
    })
}

this.props.profile.comments
也可以使用同样的方法,以防您在那里有相同的行为。一般来说,我会确保它总是被传递一个空数组或填充数组,而不是作为道具传递null。

我会在需要时选择它

const movieComments = this.props.movies ? this.props.movies : [];
this.setState({
    movie: {
        movie: {
        movieId: this.props.match.params.id,
        ...this.props.movie,           
        comments:[
            ...movieComments,
            {     
                commentID: commentID,
                by:this.props.profile.firstName,                   
                userID: this.props.auth.uid,
                createdAt: new Date(),
                comment: e.target.value               
            }               
        ]
    },
    profile:{
        ...this.props.profile,
        comments:[
            ...this.props.profile.comments,
            {                              
                movieId: this.props.match.params.id,
                commentID: commentID,                                             
                createdAt: new Date(),
                comment: e.target.value               
            }
        ]                           

    }
});

我会在需要的时候选择这个

const movieComments = this.props.movies ? this.props.movies : [];
this.setState({
    movie: {
        movie: {
        movieId: this.props.match.params.id,
        ...this.props.movie,           
        comments:[
            ...movieComments,
            {     
                commentID: commentID,
                by:this.props.profile.firstName,                   
                userID: this.props.auth.uid,
                createdAt: new Date(),
                comment: e.target.value               
            }               
        ]
    },
    profile:{
        ...this.props.profile,
        comments:[
            ...this.props.profile.comments,
            {                              
                movieId: this.props.match.params.id,
                commentID: commentID,                                             
                createdAt: new Date(),
                comment: e.target.value               
            }
        ]                           

    }
});

如果数组也是空的,它也应该工作。你确定道具是数组,而不是空的吗?事实上,我不。。。在构造器中,我正在传播。。。this.props.movie&this.props.profile,但当用户在其他组件上创建帐户时,所有默认设置都会被设置为默认设置,当用户向数据库添加新电影时,也会进行相同设置。。。所有这些数据都存在于firebase中,但数组是空的?好吧您得到了什么错误?如果数组也是空的,它应该也能工作。你确定道具是数组,而不是空的吗?事实上,我不。。。在构造器中,我正在传播。。。this.props.movie&this.props.profile,但当用户在其他组件上创建帐户时,所有默认设置都会被设置为默认设置,当用户向数据库添加新电影时,也会进行相同设置。。。所有这些数据都存在于firebase中,但数组是空的?好吧你犯了什么错误?