ReactJS 是一个用于构建用户界面的 JavaScript 库。在 React 中,组件是构建界面的基本单元。组件之间的通信是 React 开发中的一个重要方面。组件通信可以分为以下几种类型:
应用场景:父组件需要向子组件传递数据或方法。
示例代码:
// 父组件
import React from 'react';
import ChildComponent from './ChildComponent';
class ParentComponent extends React.Component {
state = {
message: 'Hello from parent'
};
render() {
return (
<div>
<ChildComponent message={this.state.message} />
</div>
);
}
}
export default ParentComponent;
// 子组件
import React from 'react';
class ChildComponent extends React.Component {
render() {
return <div>{this.props.message}</div>;
}
}
export default ChildComponent;
应用场景:子组件需要向父组件传递数据。
示例代码:
// 父组件
import React from 'react';
import ChildComponent from './ChildComponent';
class ParentComponent extends React.Component {
state = {
message: ''
};
handleMessage = (msg) => {
this.setState({ message: msg });
};
render() {
return (
<div>
<ChildComponent onMessage={this.handleMessage} />
<div>{this.state.message}</div>
</div>
);
}
}
export default ParentComponent;
// 子组件
import React from 'react';
class ChildComponent extends React.Component {
handleChange = (event) => {
this.props.onMessage(event.target.value);
};
render() {
return (
<input type="text" onChange={this.handleChange} />
);
}
}
export default ChildComponent;
应用场景:两个兄弟组件需要通信。
示例代码:
// 父组件
import React from 'react';
import SiblingComponentA from './SiblingComponentA';
import SiblingComponentB from './SiblingComponentB';
class ParentComponent extends React.Component {
state = {
message: ''
};
handleMessage = (msg) => {
this.setState({ message: msg });
};
render() {
return (
<div>
<SiblingComponentA onMessage={this.handleMessage} />
<SiblingComponentB message={this.state.message} />
</div>
);
}
}
export default ParentComponent;
// 兄弟组件A
import React from 'react';
class SiblingComponentA extends React.Component {
handleChange = (event) => {
this.props.onMessage(event.target.value);
};
render() {
return (
<input type="text" onChange={this.handleChange} />
);
}
}
export default SiblingComponentA;
// 兄弟组件B
import React from 'react';
class SiblingComponentB extends React.Component {
render() {
return <div>{this.props.message}</div>;
}
}
export default SiblingComponentB;
应用场景:多个组件需要共享状态。
示例代码:
// 创建 Context
import React from 'react';
const MyContext = React.createContext();
// 提供 Context 的组件
class ProviderComponent extends React.Component {
state = {
message: 'Hello from context'
};
render() {
return (
<MyContext.Provider value={this.state.message}>
{this.props.children}
</MyContext.Provider>
);
}
}
// 使用 Context 的组件
class ConsumerComponent extends React.Component {
render() {
return (
<MyContext.Consumer>
{(message) => <div>{message}</div>}
</MyContext.Consumer>
);
}
}
// 父组件
import React from 'react';
import ProviderComponent from './ProviderComponent';
import ConsumerComponent from './ConsumerComponent';
class App extends React.Component {
render() {
return (
<ProviderComponent>
<ConsumerComponent />
</ProviderComponent>
);
}
}
export default App;
原因:
解决方法:
this.props
正确接收 props。示例代码:
// 父组件
import React from 'react';
import ChildComponent from './ChildComponent';
class ParentComponent extends React.Component {
state = {
message: 'Hello from parent'
};
render() {
return (
<div>
<ChildComponent message={this.state.message} />
</div>
);
}
}
export default ParentComponent;
// 子组件
import React from 'react';
class ChildComponent extends React.Component {
render() {
return <div>{this.props.message}</div>;
}
}
export default ChildComponent;
原因:
解决方法:
示例代码:
// 父组件
import React from 'react';
import ChildComponent from './ChildComponent';
class ParentComponent extends React.Component {
state = {
message: ''
};
handleMessage = (msg) => {
this.setState({ message: msg });
};
render() {
return (
<div>
<ChildComponent onMessage={this.handleMessage} />
<div>{this.state.message}</div>
</div>
);
}
}
export default ParentComponent;
// 子组件
import React from 'react';
class ChildComponent extends React.Component {
handleChange = (event) => {
this.props.onMessage(event.target.value);
};
render() {
return (
<input type="text" onChange={this.handleChange} />
);
}
}
export default ChildComponent;
通过以上内容,你应该对 ReactJS 组件通信有了全面的了解,并且能够解决常见的通信问题。
领取专属 10元无门槛券
手把手带您无忧上云