React上下文没有任何价值

时间:2018-06-21 10:25:03

标签: reactjs

学习。我有以下代码

我的Package.json的条目如下;

<pre>
{
  "name": "myapp",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "bootstrap": "^3.3.7",
    "react": "^16.4.0",
    "react-dom": "^16.4.0",
    "react-router-dom": "^4.3.1",
    "react-scripts": "1.1.4",
    "webpack": "^3.11.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "babel-loader": "^7.1.4",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "webpack-dev-server": "^3.1.4"
  }
}

我创建了一个如下组件;

<pre>
import React, {Component} from "react";

export const MyContext = React.createContext();

class MyProvider extends Component{
    state ={
        isCurrentWeather : false,
        is3DayWeather : false,
        is16dayWeather : false,
        currentWeatherClicked : () => {
            this.setState(
                {
                    isCurrentWeather : true,
                    is3DayWeather : false,
                    is16dayWeather : false
                }
                )
        }
    }
    render(){
        return(
            <MyContext.Provider value = {{state : this.state}}>
                {this.props.children}
            </MyContext.Provider>
        )
    }
};
export default MyProvider

在另一个组件中,我想将此提供程序用作使用者,如下所示。但是上下文没有任何价值。甚至也没有状态信息。

 <pre>            
import React from "react";
import MyProvider, {MyContext} from "./WeatherType";

class Title extends React.Component{
    render(){
        return(
            <div>
                <h1 >My Report</h1>
                <div>
                    <MyContext.Consumer>
                        {(context) =>
                            <p>I have a {context}</p>

                        }
                    </MyContext.Consumer>
                </div>

            </div>
        );
    }
}



export default Title;

需要知道为什么我的上下文没有任何价值吗?

0 个答案:

没有答案