context html
定义: Context提供了一种方式,可以让数据在组件树中传递,而没必要一级一级手动传递。react
API : createContext(defaultValue?)。ide
使用方法:函数
首先要引入createContextthis
import React, { Component, createContext } from 'react';
而后建立一个Context spa
const BatteryContext = createContext();
而后用BatteryContext.Provider包裹组件而且传递属性值。code
<BatteryContext.Provider value={60}>
<Middle /> //子组件
</BatteryContext.Provider>
为了方便看出效果,将定义一个子组件和一个孙组件。而后不经过子组件,孙组件直接取值。htm
import React, { Component, createContext } from 'react'; const BatteryContext = createContext(); //声明一个孙组件
class Leaf extends Component { render() { return ( ) } } //声明一个子组件
class Middle extends Component { render() { return <Leaf />
} } class App extends Component { render(){ return ( <BatteryContext.Provider value={60}>
<Middle />
</BatteryContext.Provider>
); } } export default App;
孙组件须要BatteryContext.Consumer来接收值,Consumer里面不能直接渲染其余组件,而是要声明一个函数。函数的参数就是context的值。blog
class Leaf extends Component { render() { return ( <BatteryContext.Consumer> { battery => <h1>Battery : {battery}</h1>
} </BatteryContext.Consumer>
) } }
效果图;get
这样没经过Middle组件来传递值,可是Leaf组件能经过context来得到属性。这就是context的基本用法。
state = { battery: 60 }
render() { const { battery } = this.state; return ( <BatteryContext.Provider value={battery}>
<button type="button" onClick={() => this.setState({ battery: battery - 1 })} > 减减 </button>
<Middle />
</BatteryContext.Provider>
); }
import React, { Component, createContext } from 'react'; const BatteryContext = createContext(); //声明一个孙组件
class Leaf extends Component { render() { return ( <BatteryContext.Consumer> { battery => <h1>Battery : {battery}</h1>
} </BatteryContext.Consumer>
) } } //声明一个子组件
class Middle extends Component { render() { return <Leaf />
} } class App extends Component { state = { battery: 60 } render() { const { battery } = this.state; return ( <BatteryContext.Provider value={battery}>
<button type="button" onClick={() => this.setState({ battery: battery - 1 })} > 减减 </button>
<Middle />
</BatteryContext.Provider>
); } } export default App;
效果图:
这样每次点击都会使battery得数值发生变化,从而重渲染Consumer下面的元素。
const OnLineContext = createContext();
class App extends Component { state = { battery: 60, online: false } render() { const { battery, online } = this.state; return ( <BatteryContext.Provider value={battery}>
<OnLineContext.Provider value={online} >
<button type="button" onClick={() => this.setState({ battery: battery - 1 })} > 减减 </button>
<button type="button" onClick={() => this.setState({ online: !online })} > Switch </button>
<Middle />
</OnLineContext.Provider>
</BatteryContext.Provider>
); }
class Leaf extends Component { render() { return ( <BatteryContext.Consumer> { battery => ( <OnLineContext.Consumer> { online => <h1>Battery : {battery} , Online : {online.toString()}</h1>
} </OnLineContext.Consumer>
) } </BatteryContext.Consumer>
) } }
所有代码:
import React, { Component, createContext } from 'react'; const BatteryContext = createContext(); const OnLineContext = createContext(); //声明一个孙组件
class Leaf extends Component { render() { return ( //与Provider相似。Consumer也须要嵌套,顺序不重要。只要Consumer须要声明函数,因此要注意语法。
<BatteryContext.Consumer> { battery => ( <OnLineContext.Consumer> { online => <h1>Battery : {battery} , Online : {online.toString()}</h1>
} </OnLineContext.Consumer>
) } </BatteryContext.Consumer>
) } } //声明一个子组件
class Middle extends Component { render() { return <Leaf />
} } class App extends Component { state = { battery: 60, online: false } render() { const { battery, online } = this.state; //接下来声明online的Provider了。若是有多个context变量的话,只须要把Privider嵌套进来便可,顺序不重要。
return ( <BatteryContext.Provider value={battery}>
<OnLineContext.Provider value={online} >
<button type="button" onClick={() => this.setState({ battery: battery - 1 })} > 减减 </button>
<button type="button" onClick={() => this.setState({ online: !online })} > Switch </button>
<Middle />
</OnLineContext.Provider>
</BatteryContext.Provider>
); } } export default App;
效果图:
const BatteryContext = createContext(30);