react学习笔记(一)

Context上下文

通过context传递属性的方式可以大量减少通过props逐层传递属性的方式,这样可以直接减少组件之间的依赖关系

 

import React, { Component } from 'react';
import './App.css';
import PropTypes from 'prop-types'

const Topic =(props)=>{
  return(
    <Comment />
  )
}
const Comment =(props, context)=>{
  return(
    <div>comment--{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;

 

posted @ 2018-10-25 10:43  二猫喵喵  阅读(148)  评论(0)    收藏  举报