美文网首页
react中使用context(上下文)

react中使用context(上下文)

作者: 薄荷加冰 | 来源:发表于2019-12-04 15:07 被阅读0次
    import React, { Component } from 'react';
    import './App.css';
    import PropTypes from 'prop-types';
    
    const Topic = (props) => {
      return (
        <div>
          <Comment />
        </div>
      )
    }
    
    const Comment = (props, context) => {
      return (
        <div>{ context.color }</div>
      )
    }
    
    Comment.contextTypes = {
      color: PropTypes.string
    }
    
    class App extends Component {
      getChildContext() {
        return { color: "red" };
      }
      render() {
        return (
          <div className="App">
            <Topic />
          </div>
        );
      }
    }
    
    App.childContextTypes = {
      color: PropTypes.string
    }
    
    export default App;
    
    

    相关文章

      网友评论

          本文标题:react中使用context(上下文)

          本文链接:https://www.haomeiwen.com/subject/xaklgctx.html