(四)、给组件传递Props

老怪兽2023年8月29日
  • Props
  • React
小于 1 分钟

一、给 Props 设置默认值

  1. 当父组件使用子组件的时候没有传递 Props 值,但是子组件又使用 Props 参数就会报错
# TypeError: Cannot read properties of undefined (reding 'xxx')

# 不能在 undefined 上读取 xxx 的属性
  1. 这个时候就需要给 Props 设置默认值, 当使用组件的时候不传递属性就使用组件的默认值

  2. 直接使用 js 特性 结构的时候给他赋值就可以了

import React from 'react'

function BlogListItem({msg = 'hello', blog = {}}) {
    console.log(msg)
    // 接收父组件传递的值
    return <div>{blog.title}</div>
}

export default BlogListItem
Loading...