React BootStrap

参考文档:https://react-bootstrap.github.io/getting-started/introduction
安装

  1. npm install react-bootstrap@next bootstrap@5.1.0

在public/index.js中的head中添加css link

  1. <head>
  2. <link
  3. rel="stylesheet"
  4. href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css"
  5. integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We"
  6. crossorigin="anonymous"
  7. />
  8. </head>

在 Bootstrap 中,应用的所有内容通常都渲染在一个容器中。 通过给应用的根 div 元素添加 container class 属性来实现的:

  1. const App = () => {
  2. // ...
  3. return (
  4. <div className="container">
  5. // ...
  6. </div>
  7. )
  8. }

使用Table组件

  1. import { Table } from 'react-bootstrap'
  2. const Notes = ({ notes }) => (
  3. <div>
  4. <h2>Notes</h2>
  5. <Table striped>
  6. <tbody>
  7. {notes.map((note) => (
  8. <tr key={note.id}>
  9. <td>
  10. <Link to={`/notes/${note.id}`}>{note.content}</Link>
  11. </td>
  12. <td>{note.user}</td>
  13. </tr>
  14. ))}
  15. </tbody>
  16. </Table>
  17. </div>
  18. )

使用Form组件

  1. const Login = (props) => {
  2. // ...
  3. return (
  4. <div>
  5. <h2>login</h2>
  6. <Form onSubmit={onSubmit}>
  7. <Form.Group>
  8. <Form.Label>username:</Form.Label>
  9. <Form.Control type="text" name="username" />
  10. <Form.Label>password:</Form.Label>
  11. <Form.Control type="password" />
  12. <Button variant="primary" type="submit">
  13. login
  14. </Button>
  15. </Form.Group>
  16. </Form>
  17. </div>
  18. )
  19. }

使用Alert组件

  1. <div className="container">
  2. {(message &&
  3. <Alert variant="success">
  4. {message}
  5. </Alert>
  6. )}
  7. // ...
  8. </div>

使用Navbar组件

  1. <Navbar collapseOnSelect expand="lg" bg="dark" variant="dark">
  2. <Navbar.Toggle aria-controls="responsive-navbar-nav" />
  3. <Navbar.Collapse id="responsive-navbar-nav">
  4. <Nav className="mr-auto">
  5. <Nav.Link href="#" as="span">
  6. <Link style={padding} to="/">home</Link>
  7. </Nav.Link>
  8. <Nav.Link href="#" as="span">
  9. <Link style={padding} to="/notes">notes</Link>
  10. </Nav.Link>
  11. <Nav.Link href="#" as="span">
  12. <Link style={padding} to="/users">users</Link>
  13. </Nav.Link>
  14. <Nav.Link href="#" as="span">
  15. {user
  16. ? <em style={padding}>{user} logged in</em>
  17. : <Link style={padding} to="/login">login</Link>
  18. }
  19. </Nav.Link>
  20. </Nav>
  21. </Navbar.Collapse>
  22. </Navbar>

Material UI

官方文档:https://material-ui.com/zh/getting-started/installation/
安装

  1. npm install @material-ui/core

在public/index.js的head中添加下列代码,引入Roboto字体

  1. <head>
  2. <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />
  3. // ...
  4. </head>

所有要渲染的组件都要放在Container组件中

  1. import Container from '@material-ui/core/Container'
  2. const App = () => {
  3. // ...
  4. return (
  5. <Container>
  6. // ...
  7. </Container>
  8. )
  9. }

使用Table组件

  1. const Notes = ({notes}) => (
  2. <div>
  3. <h2>Notes</h2>
  4. <TableContainer component={Paper}>
  5. <Table>
  6. <TableBody>
  7. {notes.map(note => (
  8. <TableRow key={note.id}>
  9. <TableCell>
  10. <Link to={`/notes/${note.id}`}>{note.content}</Link>
  11. </TableCell>
  12. <TableCell>
  13. {note.user}
  14. </TableCell>
  15. </TableRow>
  16. ))}
  17. </TableBody>
  18. </Table>
  19. </TableContainer>
  20. </div>
  21. )

这些组件要一个一个引入

  1. import {
  2. Container,
  3. Table,
  4. TableBody,
  5. TableCell,
  6. TableContainer,
  7. TableRow,
  8. Paper,
  9. } from '@material-ui/core'

使用TextFieldButton组件

  1. <div>
  2. <h2>login</h2>
  3. <form onSubmit={onSubmit}>
  4. <div>
  5. <TextField label="username" />
  6. </div>
  7. <div>
  8. <TextField label="password" type='password' />
  9. </div>
  10. <div>
  11. <Button variant="contained" color="primary" type="submit">
  12. login
  13. </Button>
  14. </div>
  15. </form>
  16. </div>

使用Alert组件
先安装lab包

  1. npm install @material-ui/lab
  1. import { Alert } from '@material-ui/lab'
  1. <div>
  2. {(message &&
  3. <Alert severity="success">
  4. {message}
  5. </Alert>
  6. )}
  7. </div>

使用Appbar实现导航栏

  1. <AppBar position="static">
  2. <Toolbar>
  3. <Button color="inherit" component={Link} to="/">
  4. home
  5. </Button>
  6. <Button color="inherit" component={Link} to="/notes">
  7. notes
  8. </Button>
  9. <Button color="inherit" component={Link} to="/users">
  10. users
  11. </Button>
  12. {user
  13. ? <em>{user} logged in</em>
  14. : <Button color="inherit" component={Link} to="/login">
  15. login
  16. </Button>
  17. }
  18. </Toolbar>
  19. </AppBar>

对比

npm包流行程度对比查询:https://www.npmtrends.com/
其他库:
https://bulma.io/
https://ant.design/
https://get.foundation/
https://chakra-ui.com/
https://tailwindcss.com/
https://semantic-ui.com/

Styled components

styled-components允许使用tagged templates语法定义样式
安装

  1. npm install styled-components

将普通元素定义为带样式的组件

  1. import styled from 'styled-components'
  2. const Button = styled.button`
  3. background: Bisque;
  4. font-size: 1em;
  5. margin: 1em;
  6. padding: 0.25em 1em;
  7. border: 2px solid Chocolate;
  8. border-radius: 3px;
  9. `
  10. const Input = styled.input`
  11. margin: 0.25em;
  12. `
  13. const Page = styled.div`
  14. padding: 1em;
  15. background: papayawhip;
  16. `
  17. const Navigation = styled.div`
  18. background: BurlyWood;
  19. padding: 1em;
  20. `
  21. const Footer = styled.div`
  22. background: Chocolate;
  23. padding: 1em;
  24. margin-top: 1em;
  25. `

使用定义了样式的组件(元素)

  1. const Login = (props) => {
  2. // ...
  3. return (
  4. <div>
  5. <h2>login</h2>
  6. <form onSubmit={onSubmit}>
  7. <div>
  8. username:
  9. <Input />
  10. </div>
  11. <div>
  12. password:
  13. <Input type='password' />
  14. </div>
  15. <Button type="submit" primary=''>login</Button>
  16. </form>
  17. </div>
  18. )
  19. }
  20. const App = () => {
  21. // ...
  22. return (
  23. <Page>
  24. <Navigation>
  25. <Link style={padding} to="/">home</Link>
  26. <Link style={padding} to="/notes">notes</Link>
  27. <Link style={padding} to="/users">users</Link>
  28. {user
  29. ? <em>{user} logged in</em>
  30. : <Link style={padding} to="/login">login</Link>
  31. }
  32. </Navigation>
  33. <Switch>
  34. <Route path="/notes/:id">
  35. <Note note={note} />
  36. </Route>
  37. <Route path="/notes">
  38. <Notes notes={notes} />
  39. </Route>
  40. <Route path="/users">
  41. {user ? <Users /> : <Redirect to="/login" />}
  42. </Route>
  43. <Route path="/login">
  44. <Login onLogin={login} />
  45. </Route>
  46. <Route path="/">
  47. <Home />
  48. </Route>
  49. </Switch>
  50. <Footer>
  51. <em>Note app, Department of Computer Science 2021</em>
  52. </Footer>
  53. </Page>
  54. )
  55. }

样式化组件在最近一段时间内一直受到欢迎,很多人认为这是定义样式到 React 应用的最佳方式。