首页 > 其他分享 >好客租房57-props深入(4props的默认值)

好客租房57-props深入(4props的默认值)

时间:2023-02-10 15:31:41浏览次数:42  
标签:react render 57 4props colors PropTypes props 组件 默认值


1给props设置默认值

   

//导入react
import React from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'
//导入组件
// 约定1:类组件必须以大写字母开头
// 约定2:类组件应该继承react.component父类 从中可以使用父类的方法和属性
// 约定3:组件必须提供render方法
// 约定4:render方法必须有返回值

const App = (props) => {
const arr = props.colors
const lis = arr.map((item, index) => <li key={index}>{item}</li>)
return <ul>{lis}</ul>
}
App.propTypes = {
colors: PropTypes.array,
a: PropTypes.number,
//节点
tag: PropTypes.element,
}
App.defaultProps = {
colors: ['red', 'blue'],
}

ReactDOM.render(<App></App>, document.getElementById('root'))


 

标签:react,render,57,4props,colors,PropTypes,props,组件,默认值
From: https://blog.51cto.com/u_15460007/6049461

相关文章