给无状态的纯UI组件应用样式

请保持样式远离那些离不开state的组件. 比如路由, 视图, 容器, 表单, 布局等等不应该有任何的样式或者css class出现在组件上.
相反, 这些复杂的业务组件应该有一些带有基本功能的无状态UI组件组成.

没有任何样式/classNames的Form组件, 仅有纯的组件组合而成.

  1. class SampleComponent extends Component {
  2. render() {
  3. return (
  4. <form onSubmit={this.handleSubmit}>
  5. <Heading children='Sign In'/>
  6. <Input
  7. name='username'
  8. value={username}
  9. onChange={this.handleChange}/>
  10. <Input
  11. type='password'
  12. name='password'
  13. value={password}
  14. onChange={this.handleChange}/>
  15. <Button
  16. type='submit'
  17. children='Sign In'/>
  18. </form>
  19. )
  20. }
  21. }
  22. // 表达组件(带样式)
  23. const Button = ({
  24. ...props
  25. }) => {
  26. const sx = {
  27. fontFamily: 'inherit',
  28. fontSize: 'inherit',
  29. fontWeight: 'bold',
  30. textDecoration: 'none',
  31. display: 'inline-block',
  32. margin: 0,
  33. paddingTop: 8,
  34. paddingBottom: 8,
  35. paddingLeft: 16,
  36. paddingRight: 16,
  37. border: 0,
  38. color: 'white',
  39. backgroundColor: 'blue',
  40. WebkitAppearance: 'none',
  41. MozAppearance: 'none'
  42. }
  43. return (
  44. <button {...props} style={sx}/>
  45. )
  46. }