react Context

import React, { useState, useEffect, useContext } from "react";
import axios from "axios";
const l = console.log;

const BodyContext = React.createContext("body");

function Test(props) {
  // return <Hello body={`hello world`} />;
  return (
    <BodyContext.Provider value="hello ajanuw">
      <Hello />
    </BodyContext.Provider>
  );
}

function Hello({ body }) {
  return <World />;
}
function World(props) {
  // return <div>{props.body}</div>;

  // return (
  //   <BodyContext.Consumer>
  //     {body => <div>{body}</div>}
  //   </BodyContext.Consumer>
  // );
  const body = useContext(BodyContext);
  return <div>{body}</div>;
}
export default Test;
原文地址:https://www.cnblogs.com/ajanuw/p/10142382.html