4.react useContext使用与常见问题

1. 在函数组件实现跨组件通信的方式
2. 注册Context将value传递给子组件

let MyContext = React.createContext('默认值');

let value = useContext(MyContext)

4.react useContext使用与常见问题_第1张图片

DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Documenttitle>
  <style>style>
  <script src="../react.development.js">script>
  <script src="../react-dom.development.js">script>
  <script src="../babel.min.js">script>
  <script src="../lodash.min.js">script>
head>

<body>
  <div id="app">div>
  <script type="text/babel">
    // 实现三层组件的跨组件通信
    let Welcome = (props) => {  
      return (
        <div>
          hello Welcome
          <MyContext.Provider value="welcome的问候~~~">
            <Head />
          </MyContext.Provider>
        </div>
      );
    }
    let Head = () => {
      return (
        <div>
          hello Head
          <Title />
        </div>
      );
    }
    let Title = () => {
      let value = useContext(MyContext);
      return (
        <div>
          hello Title, { value }
        </div>
      );
    }
    
    let element = (
      <Welcome />
    );
    root.render(element);
  script>
body>

html>

你可能感兴趣的:(#,S2.React,hooks.formik,react.js,javascript,ecmascript)