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

阅读 83

2022-05-23


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'))





精彩评论(0)

0 0 举报