TypeError:无法在反应中读取null的属性“ first_name”

时间:2020-09-27 11:47:05

标签: reactjs

  export function UserDisplay(props) {
    const {user, includeFullName} = props
    const nameDisplay = includeFullName === true ? `${user.first_name} ${user.last_name}` : null  

    return <React.Fragment>
          {nameDisplay}
      </React.Fragment>
  }

在这里,当我检查值时,它像这样:

const name = user.first_name
console.log(name)

它打印日志,但是在代码中不起作用,为什么?

 const nameDisplay = includeFullName === true ? `${user.first_name} ${user.last_name}` : null

收到错误TypeError: Cannot read property 'first_name' of null

0 个答案:

没有答案