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

2 years ago
  1. import React from 'react'
  2. // prop-types 里面有各种各样的内置的效验规则
  3. import PropTypes from 'prop-types'
  4. function Test ({list}) {
  5. return (
  6. <div>
  7. {list.map(item => <p key={item}>{item}</p>)}
  8. </div>
  9. )
  10. }
  11. Test.propTypes = { // 这里的 propTypes 首字母要小写
  12. // 定义各种规则
  13. // 给 Test 的 list 数组定义规则
  14. list: PropTypes.array.isRequired // 限定这里的 list 参数的类型必须是数组类型,添加了 isRequired 就是必填项的意思,没填会报错
  15. }
  16. class App extends React.Component {
  17. render () {
  18. return (
  19. <div>
  20. <Test list={[1, 2, 3]} />
  21. </div>
  22. )
  23. }
  24. }
  25. export default App