0
点赞
收藏
分享

微信扫一扫

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

飞鸟不急 2022-05-23 阅读 82


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 条评论