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.

38 lines
881 B

2 years ago
  1. import React from 'react'
  2. /*
  3. 第一种使用 defaultProps
  4. 第二种函数参数默认值推荐方案 {pageSize = 10 }
  5. 区别第一种在用的时候组件内部已经有了 pageSize 这个 prop第二种只要传递的时候组件内部才有这个 prop
  6. */
  7. // 第二种:函数参数默认值(推荐方案) {pageSize = 10 }
  8. function Test ({pageSize = 10 }) {
  9. return (
  10. <div>
  11. this is test
  12. {pageSize}
  13. </div>
  14. )
  15. }
  16. // 设置默认值
  17. // 第一种:使用 defaultProps
  18. /* Test.defaultProps = {
  19. pageSize: 10 // 如果传数据,pageSize 就以传入的为主,如果不传就是 10
  20. } */
  21. class App extends React.Component {
  22. render () {
  23. return (
  24. <div>
  25. <Test pageSize={ 20 }/>
  26. </div>
  27. )
  28. }
  29. }
  30. export default App