react学习

xiaoxiao2025-05-30  21

react学习----Context上下文

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

import React, { Component } from 'react'; 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;
转载请注明原文地址: https://www.6miu.com/read-5030943.html

最新回复(0)