3. React 组件传递参数(props) Context props 深入 children 校验 校验-约束规则 默认值

249 阅读1分钟

1. React 组件传递参数(props)


2. React 父组件 向 子组件 和 子组件 向 父组件 传递参数


3. React 兄弟组件 传递参数


4. React 中 Context 来传递数据

// 创建 context 来传递参数  Provider 提供数据  Consumer 消费数据
const {Provider, Consumer} = React.createContext()

5. React 中 props 深入 children 校验 校验-约束规则 默认值

yarn add prop-types  或者 npm i props-types

// 导入 prop-types 的包
import PropTypes from 'prop-types'

// 定义校验的数据类型
App.propTypes = {
  colors: PropTypes.array
}