导航


HTML

CSS

JavaScript

浏览器 & 网络

版本管理

框架

构建工具

TypeScript

性能优化

软实力

算法

UI、组件库

Node

业务技能

针对性攻坚

AI


props.children

  1. 如果 Container 标签有内容,React 会在 props 上增加 children 属性
  2. 如果 Container 标签内部有非元素内容children: 就是这个非元素内容
    1. <Container>123</Container>
  3. 如果 Container 内部有单个元素内容, children: React元素对象
    1. <Container><h1>123</h1></Container>
  4. 如果 Container 内部有多个元素内容, children:[React元素对象1, React元素对象2,"123",...]
    1. <Container><h1>123</h1><h2>234</h2></Container>
class Container extends React.Component {
  render() {
    return (
      <div className="container">
        <div className="header">
          { this.props.header }
        </div>
        <div className="sidebar">
          { this.props.sidebar }
        </div>
        <div className="main">
          { this.props.main }
        </div>
      </div>
    )
  }
}

class Header extends React.Component {
  render() {
    return (
      <p>HEADER</p>
    )
  }
}

class Sidebar extends React.Component {
  render() {
    return (
      <p>SIDEBAR</p>
    )
  }
}

class Main extends React.Component {
  render() {
    return (
      <p>MAIN</p>
    )
  }
}

class App extends React.Component {
  render() {
    return (
      // <Container />
      <Container
        header={ <Header /> }
        sidebar={ <Sidebar /> }
        main={ <Main /> }
      />
    )
  }
}

ReactDOM.render(
  <App />,
  document.getElementById('app')
)

props 可以传入 JSX

class Container extends React.Component {
  render() {
    return (
      <div className="container">
        <div className="header">
          { this.props.header }
        </div>
        <div className="sidebar">
          { this.props.sidebar }
        </div>
        <div className="main">
          { this.props.main }
        </div>
      </div>
    )
  }
}

class Header extends React.Component {
  render() {
    return (
      <p>HEADER</p>
    )
  }
}

class Sidebar extends React.Component {
  render() {
    return (
      <p>SIDEBAR</p>
    )
  }
}

class Main extends React.Component {
  render() {
    return (
      <p>MAIN</p>
    )
  }
}

class App extends React.Component {
  render() {
    return (
      <Container
        header={ <Header /> }
        sidebar={ <Sidebar /> }
        main={ <Main /> }
      />
    )
  }
}

ReactDOM.render(
  <App />,
  document.getElementById('app')
)

CSS Module

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

h1,
p {
  margin: 0;
  font-weight: normal;
}

.container {
  position: relative;
  width: 100%;
  height: 100%;
}

.header {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 3;
  width: 100%;
  height: 60px;
  background-color: #000;
}

.sidebar {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  width: 300px;
  height: 100%;
  padding-top: 80px;
  box-sizing: border-box;
  background-color: orange;
}

.main {
  position: absolute;
  box-sizing: border-box;
  top: 0;
  left: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  padding: 80px 0 0 320px;
  background-color: green;
}
import styles from './12.module.css' // 通过模块化方式导入样式
class Container extends React.Component {
  render() {
    return (
      <div className={ styles.container }>
        <div className={ styles.header }>
          { this.props.header }
        </div>
        <div className={ styles.sidebar }>
          { this.props.sidebar }
        </div>
        <div className={ styles.main }>
          { this.props.main }
        </div>
      </div>
    )
  }
}

class Header extends React.Component {
  render() {
    return (
      <p>HEADER</p>
    )
  }
}

class Sidebar extends React.Component {
  render() {
    return (
      <p>SIDEBAR</p>
    )
  }
}

class Main extends React.Component {
  render() {
    return (
      <p>MAIN</p>
    )
  }
}

class App extends React.Component {
  render() {
    return (
      <Container
        header={ <Header /> }
        sidebar={ <Sidebar /> }
        main={ <Main /> }
      />
    )
  }
}

ReactDOM.render(
  <App />,
  document.getElementById('app')
)

多层组合

import styles from './13.module.css';
function Modal(props) {
  return (
    <div className={ styles.modal }>
      <header className={ styles.modalHeader }>
        <h1>{ props.headerTitle }</h1>
      </header>
      <div className={ styles.modalContent }>
        {/* 展示 modal 双标签之间的所有内容(类似 Vue 默认插槽) */}
        { props.children }
      </div>
    </div>
  )
}
function Alert(props) {
  return (
    <Modal headerTitle={ props.headerTitle }>
      <p>{ props.alertText }</p>
    </Modal>
  )
}
function LoginModal(props) {
  return (
    <Modal headerTitle="登录">
      <form action=''>
        <p>
          <input type="text" placeholder='用户名' />
        </p>
        <p>
          <input type="text" placeholder='密码' />
        </p>
        <p>
          <button>登录</button>
        </p>
      </form>
    </Modal>
  )
}
function WelcomeAlert() {
  return (
    <Alert
      headerTitle="欢迎您"
      alertText="亲爱的用户您好"
    />
  )
}
function App() {
  return (
    <div>
      {/* <WelcomeAlert /> */}
      <LoginModal />
    </div>
  )
}

ReactDOM.render(<App />, document.getElementById('app'));
html,
body {
  margin: 0;
  padding: 0;
}
h1,
p {
  margin: 0;
}
.modal {
  position: fixed;
  top: 60px;
  left: 50%;
  width: 300px;
  margin-left: -150px;
  box-shadow: 1px 3px 5px #999;
  border-radius: 10px;
  overflow: hidden;
}

.modalHeader {
  height: 44px;
  padding: 0 15px;
  box-sizing: border-box;
  line-height: 44px;
  background-color: orange;
}

.modalHeader h1 {
  font-size: 16px;
}

.modalContent {
  padding: 15px;
  box-sizing: border-box;
  background-color: #fff;
}