小案例(card)

  1. import React from 'react'
  2. import './App.css'
  3. import Card from './Card'
  4. class App extends React.Component{
  5. render(){
  6. console.log(this.props)
  7. return (
  8. <div className='app'>
  9. <Card title='Card title' more={<a href="http://baidu.com">More</a>} >
  10. <p>每个人心中都有一团火, 路过的人只看到烟。</p>
  11. <p style={{textAlign:'right'}}>——文森特·梵高</p>
  12. </Card>
  13. </div>
  14. )
  15. }
  16. }
  17. export default App

可复用子组件

  1. import React from 'react'
  2. import PropTypes from 'prop-types'
  3. class Card extends React.Component{
  4. render(){
  5. return (
  6. <div className='card'>
  7. <div className='header'>
  8. <h4>{this.props.title}</h4>
  9. <div className='more'>{this.props.more}</div>
  10. </div>
  11. <div className='main'>
  12. {this.props.children}
  13. </div>
  14. </div>
  15. )
  16. }
  17. }
  18. Card.defaultProps = {
  19. title:'请输入标题',
  20. more:<a href="#">More</a>,
  21. children:'请传入内容'
  22. }
  23. Card.propTypes = {
  24. title:PropTypes.string,
  25. more:PropTypes.element,
  26. children:PropTypes.oneOfType([
  27. PropTypes.object,
  28. PropTypes.array,
  29. ])
  30. }
  31. export default Card