// 父组件
class Parent extends Component {
constructor() {
super();
this.state = {
value: '',
}
}
handleChange = e => {
this.value = e.target.value;
}
handleClick = () => {
this.setState({
value: this.value,
})
}
render() {
return (
<div>
我是parent
<input onChange={this.handleChange} />
<div className="button" onClick={this.handleClick}>通知</div>
<div>
<Child value={this.state.value} />
</div>
</div>
);
}
}
// 子组件
class Child extends Component {
render() {
const { value } = this.props;
return (
<div>
我是Child,得到传下来的值:{value}
</div>
);
}
}
// parent
class Parent extends Component {
constructor() {
super();
this.state = {
value: '',
}
}
setValue = value => {
this.setState({
value,
})
}
render() {
return (
<div>
<div>我是parent, Value是:{this.state.value}</div>
<Child setValue={this.setValue} />
</div>
);
}
}
class Child extends Component {
handleChange = e => {
this.value = e.target.value;
}
handleClick = () => {
const { setValue } = this.props;
setValue(this.value);
}
render() {
return (
<div>
我是Child
<div className="card">
state 定义在 parent
<input onChange={this.handleChange} />
<div className="button" onClick={this.handleClick}>通知</div>
</div>
</div>
);
}
}
// parent
class Parent extends Component {
onChange = value => {
console.log(value, '来自 child 的 value 变化');
}
render() {
return (
<div>
<div>我是parent
<Child onChange={this.onChange} />
</div>
);
}
}
class Child extends Component {
constructor() {
super();
this.state = {
childValue: ''
}
}
childValChange = e => {
this.childVal = e.target.value;
}
childValDispatch = () => {
const { onChange } = this.props;
this.setState({
childValue: this.childVal,
}, () => { onChange(this.state.childValue) })
}
render() {
return (
<div>
我是Child
<div className="card">
state 定义在 child
<input onChange={this.childValChange} />
<div className="button" onClick={this.childValDispatch}>通知</div>
</div>
</div>
);
}
}
// container
class Container extends Component {
constructor() {
super();
this.state = {
value: '',
}
}
setValue = value => {
this.setState({
value,
})
}
render() {
return (
<div>
<A setValue={this.setValue}/>
<B value={this.state.value} />
</div>
);
}
}
// 兄弟A
class A extends Component {
handleChange = (e) => {
this.value = e.target.value;
}
handleClick = () => {
const { setValue } = this.props;
setValue(this.value);
}
render() {
return (
<div className="card">
我是Brother A, <input onChange={this.handleChange} />
<div className="button" onClick={this.handleClick}>通知</div>
</div>
)
}
}
// 兄弟B
const B = props => (
<div className="card">
我是Brother B, value是:
{props.value}
</div>
);
export default B;
constructor() {
super();
this.state = {
value: '',
};
}
setValue = value => {
this.setState({
value,
})
}
getChildContext() { // 必需
return {
value: this.state.value,
setValue: this.setValue,
};
}
render() {
return (
<div>
<AParent />
<BParent />
</div>
);
}
}
// 必需
Context.childContextTypes = {
value: PropTypes.string,
setValue: PropTypes.func,
};
// A 的 parent
class AParent extends Component {
render() {
return (
<div className="card">
<A />
</div>
);
}
}
// A
class A extends Component {
handleChange = (e) => {
this.value = e.target.value;
}
handleClick = () => {
const { setValue } = this.context;
setValue(this.value);
}
render() {
return (
<div>
我是parentA 下的 A, <input onChange={this.handleChange} />
<div className="button" onClick={this.handleClick}>通知</div>
</div>
);
}
}
// 必需
A.contextTypes = {
setValue: PropTypes.func,
};
// B 的 parent
class BParent extends Component {
render() {
return (
<div className="card">
<B />
</div>
);
}
}
// B
class B extends Component {
render() {
return (
<div>
我是parentB 下的 B, value是:
{this.context.value}
</div>
);
}
}
B.contextTypes = {
value: PropTypes.string,
};
// 发布订阅类
class EventEmitter {
_event = {}
// on 函数用于绑定
on(eventName, handle) {
let listeners = this._event[eventName];
if(!listeners || !listeners.length) {
this._event[eventName] = [handle];
return;
}
listeners.push(handle);
}
// off 用于移除
off(eventName, handle) {
let listeners = this._event[eventName];
this._event[eventName] = listeners.filter(l => l !== handle);
}
// emit 用于分发消息
emit(eventName, ...args) {
const listeners = this._event[eventName];
if(listeners && listeners.length) {
for(const l of listeners) {
l(...args);
}
}
}
}
const event = new EventEmitter;
export { event };
// Container
import A from './a';
import B from './b';
const Listener = () => {
return (
<div>
<A />
<B />
</div>
);
};
export default Listener;
// 兄弟组件 A
import { event } from './eventEmitter';
class A extends Component {
handleChange = e => {
this.value = e.target.value;
}
handleClick = () => {
event.emit('dispatch', this.value);
}
render() {
return (
<div className="card">
我是Brother A, <input onChange={this.handleChange} />
<div className="button" onClick={this.handleClick}>通知</div>
</div>
)
}
}
// 兄弟组件 B
import { event } from './eventEmitter';
class B extends Component {
state = {
value: ''
}
componentDidMount() {
event.on('dispatch', this.valueChange);
}
componentWillUnmount() {
event.off('dispatch', this.valueChange);
}
valueChange = value => {
this.setState({
value,
})
}
render() {
return (
<div className="card">
我是Brother B, value是:
{this.state.value}
</div>
);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有