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;
posted @ 2018-12-19 11:58  Ajanuw  阅读(143)  评论(0编辑  收藏  举报