You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
746 B
29 lines
746 B
import React from 'react'
|
|
// prop-types 里面有各种各样的内置的效验规则
|
|
import PropTypes from 'prop-types'
|
|
|
|
function Test ({list}) {
|
|
return (
|
|
<div>
|
|
{list.map(item => <p key={item}>{item}</p>)}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
Test.propTypes = { // 这里的 propTypes 首字母要小写
|
|
// 定义各种规则
|
|
// 给 Test 的 list 数组定义规则
|
|
list: PropTypes.array.isRequired // 限定这里的 list 参数的类型必须是数组类型,添加了 isRequired 就是必填项的意思,没填会报错
|
|
}
|
|
|
|
class App extends React.Component {
|
|
render () {
|
|
return (
|
|
<div>
|
|
<Test list={[1, 2, 3]} />
|
|
</div>
|
|
)
|
|
}
|
|
}
|
|
|
|
export default App
|