在开发过程中,我们会经常遇到需要组件之间相互进行通信:
- 比如App可能使用了多个Header,每个地方的Header展示的内容不同,那么我们就需要使用者传递给Header一些数据,让其进行展示;
- 又比如我们在Main中一次性请求了Banner数据和ProductList数据,那么就需要传递给他们来进行展示;
- 也可能是子组件中发生了事件,需要由父组件来完成某些操作,那就需要子组件向父组件传递事件;
总之,在一个React项目中,组件之间的通信是非常重要的环节; 父组件在展示子组件,可能会传递一些数据给子组件:
对于传递给子组件的数据,有时候我们可能希望进行验证,特别是对于大型项目来说:
更多的验证方式,可以参考官网:https://zh-hans.reactjs.org/docs/typechecking-with-proptypes.html
如果没有传递,我们希望有默认值呢?
某些情况,我们也需要子组件向父组件传递消息:
将计数器案例进行拆解;
import React, {PureComponent} from 'react';
class CounterButton extends PureComponent {
constructor(props) {
super(props);
}
render() {
return (
<button onClick={e => this.counterIncrement()}>CounterButton里的+1</button>
)
}
counterIncrement() {
const {increment} = this.props
increment()
// this.props.increment()
}
}
class App extends PureComponent {
constructor(props) {
super(props);
this.state = {
counter: 0
}
}
render() {
return (
<div>
<h2>当前计数: {this.state.counter}</h2>
<button onClick={e => this.increment()}>App里的+</button>
<br/>
<CounterButton increment={e => this.increment()} />
</div>
);
}
increment() {
this.setState({
counter: this.state.counter + 1
})
}
}
export default App ;
import React, { Component } from 'react';
function ProfileHeader (props) {
return (
<div>
<h2>用户昵称: {props.nickname}</h2>
<h2>用户等级: {props.level}</h2>
</div>
)
}
function Profile (props) {
return (
<div>
{/* */}
<ProfileHeader {...props} />
<ul>
<li>设置1</li>
<li>设置2</li>
<li>设置3</li>
<li>设置4</li>
</ul>
</div>
)
}
class App extends Component {
constructor(props) {
super(props)
this.state = {
nickname: 'zepp',
level: 99
}
}
render() {
const {nickname, level} = this.state
return (
<div>
app
<Profile nickname={nickname} level={level} />
</div>
);
}
}
export default App;
非父子组件数据的共享:
但是,如果层级更多的话,一层层传递是非常麻烦,并且代码是非常冗余的:
什么时候使用Context.Consumer呢?
import React, {Component} from 'react';
// 创建Context对象
const UserContext = React.createContext({
nickname: 'bbb',
level: -1
})
/* function ProfileHeader (props) {
return (
用户昵称:
用户等级:
)
} */
class ProfileHeader extends Component {
render() {
console.log('====================================');
console.log(this.context);
console.log('====================================');
return (
<div>
<h2>用户昵称:{this.context.nickname} </h2>
<h2>用户等级:{this.context.level} </h2>
</div>
)
}
}
ProfileHeader.contextType = UserContext
function Profile(props) {
return (
<div>
{/* */}
<ProfileHeader/>
<ul>
<li>设置1</li>
<li>设置2</li>
<li>设置3</li>
<li>设置4</li>
</ul>
</div>
)
}
class App extends Component {
constructor(props) {
super(props)
this.state = {
nickname: 'zepp222',
level: 99
}
}
render() {
const {nickname, level} = this.state
return (
<div>
app
<UserContext.Provider value={this.state}>
<Profile/>
</UserContext.Provider>
</div>
);
}
}
export default App;
import React, {Component} from "react";
// 创建Context对象
const UserContext = React.createContext({
nickname: "bbb",
level: -1,
});
function ProfileHeader(props) {
return (
<UserContext.Consumer>
{(value) => {
return (
<div>
<h2>用户昵称:{value.nickname} </h2>
<h2>用户等级:{value.level} </h2>
</div>
);
}}
</UserContext.Consumer>
);
}
/* class ProfileHeader extends Component {
render() {
console.log('====================================');
console.log(this.context);
console.log('====================================');
return (
用户昵称:{this.context.nickname}
用户等级:{this.context.level}
)
}
} */
// ProfileHeader.contextType = UserContext;
function Profile(props) {
return (
<div>
<ProfileHeader/>
<ul>
<li>设置1</li>
<li>设置2</li>
<li>设置3</li>
<li>设置4</li>
</ul>
</div>
);
}
class App extends Component {
constructor(props) {
super(props);
this.state = {
nickname: "zepp",
level: 99,
};
}
render() {
return (
<div>
app
<UserContext.Provider value={this.state}>
<Profile/>
</UserContext.Provider>
</div>
);
}
}
export default App;
import React, { Component } from "react";
// 创建Context对象
const UserContext = React.createContext({
nickname: "bbb",
level: -1,
});
const ThemeContext = React.createContext({
color: "black",
});
function ProfileHeader(props) {
return (
<UserContext.Consumer>
{(value) => {
return (
<ThemeContext.Consumer>
{(theme) => {
return (
<div>
<h2>用户昵称:{value.nickname} </h2>
<h2>用户等级:{value.level} </h2>
<h2>颜色: {theme.color}</h2>
</div>
);
}}
</ThemeContext.Consumer>
);
}}
</UserContext.Consumer>
);
}
/* class ProfileHeader extends Component {
render() {
console.log('====================================');
console.log(this.context);
console.log('====================================');
return (
用户昵称:{this.context.nickname}
用户等级:{this.context.level}
)
}
} */
ProfileHeader.contextType = UserContext;
function Profile(props) {
return (
<div>
<ProfileHeader />
<ul>
<li>设置1</li>
<li>设置2</li>
<li>设置3</li>
<li>设置4</li>
</ul>
</div>
);
}
class App extends Component {
constructor(props) {
super(props);
this.state = {
nickname: "zepp",
level: 99,
};
}
render() {
return (
<div>
app
<UserContext.Provider value={this.state}>
<ThemeContext.Provider value={{ color: "red" }}>
<Profile />
</ThemeContext.Provider>
</UserContext.Provider>
</div>
);
}
}
export default App;