初学react,怎么优化这段代码,能不能帮忙改一下

componentWillReceiveProps =(newProps)=> {

        const _self = this;
        //console.log('基本信息组件接受参数');
        //console.log('newProps = ', newProps['basicInformationData']);
        let videoLength = 0;
        let videoSize = 0;
        let distanceCreationTime = 0;

        let tags = []

        if( newProps['basicInformationData'] !== undefined) {
            if ( newProps['basicInformationData']['distance_time'] !== undefined ) {
                distanceCreationTime = newProps['basicInformationData']['distance_time']
            }

            if ( newProps['basicInformationData']['duration'] !== undefined ) {
                videoLength = newProps['basicInformationData']['duration']
            }

            if ( newProps['basicInformationData']['size'] !== undefined ) {
                videoSize = newProps['basicInformationData']['size']
            }

            if ( newProps['basicInformationData']['tags'] !== undefined ) {
                tags = newProps['basicInformationData']['tags']
            }
        }

        _self.setState({
            tags: tags, // 任务所有名称
            videoLength: videoLength,
            videoSize: videoSize,
            distanceCreationTime: distanceCreationTime
        })


    }

下面这段怎么优化?

_self.setState({
            tags: tags, // 任务所有名称
            videoLength: videoLength,
            videoSize: videoSize,
            distanceCreationTime: distanceCreationTime
        })

这个if怎么优化

if( newProps['basicInformationData'] !== undefined) {
            if ( newProps['basicInformationData']['distance_time'] !== undefined ) {
                distanceCreationTime = newProps['basicInformationData']['distance_time']
            }

            if ( newProps['basicInformationData']['duration'] !== undefined ) {
                videoLength = newProps['basicInformationData']['duration']
            }

            if ( newProps['basicInformationData']['size'] !== undefined ) {
                videoSize = newProps['basicInformationData']['size']
            }

            if ( newProps['basicInformationData']['tags'] !== undefined ) {
                tags = newProps['basicInformationData']['tags']
            }
        }
阅读 1.4k
1 个回答
const obj = newProps['basicInformationData'] || {}
let distanceCreationTime = obj['distance_time'] || 0;
let videoSize = obj['size'] || 0;
let videoLength =  obj['duration'] || 0;
let tags = obj['tags'] || []
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题