reactの親コンポーネントがサブコンポーネントを呼び出す方法
5173 ワード
1.refを直接使用して取得する
2.サブコンポーネントが上位コンポーネントである場合、refによりサブコンポーネントインスタンスメソッドを取得します.
サブコンポーネントは、 親コンポーネントは、
ここでは後者に属するが、特にサブアセンブリが
上のchildComponentがwithRouterに包まれた後、
公式に提供された
私たちが何を得る必要があるかを知っていれば、それはやりやすいです.私は
perfect ! 問題は解決しました.このようにして、私はどのように高次コンポーネントを使っても、どのようにして私たちの
もちろん完璧でも相対的であり、例えば上記のスキームでは、
転載先:https://www.cnblogs.com/jcxfighting/p/10512005.html
import React, {Component} from 'react';
export default class Parent extends Component {
render() {
return(
)
}
onRef = (ref) => {
this.child = ref
}
click = (e) => {
this.child.myName()
}
}
class Child extends Component {
componentDidMount(){
this.props.onRef(this)
}
myName = () => alert('xiaohesong')
render() {
return ('woqu')
}
}
2.サブコンポーネントが上位コンポーネントである場合、refによりサブコンポーネントインスタンスメソッドを取得します.
props
によって解決される親コンポーネントの情報を取得する必要がある.ref
によって解決されるサブコンポーネントの情報を知る必要がある.ここでは後者に属するが、特にサブアセンブリが
@connect
@withRouter
で包まれたコンポーネント(実際にはほとんどのコンポーネントがこの2つに包まれている)のような高次コンポーネントであることに特化している.具体的な例は以下の通りである.@withRouter
export default class childComponent extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (this is childComponent)
}
}
@withRouter
export default class parentComponent extends Component {
constructor(props) {
super(props);
this.state = {};
}
render () {
return { this.childCp = v; }}/>
}
}
上のchildComponentがwithRouterに包まれた後、
parentComponent
でref
で取得したのはchildComponent
ではなく、withRouter
コンポーネントです.これは気まずいですが、ほとんどの場合、自分が書いたコンポーネントのインスタンスを取得する必要があります.ref
によって取得されたのはchildComponent
ではなく、原理的には正しいが、childComponent
コンポーネントであれば問題があり、倫理に反して知っている.公式に提供された
ref
では、私たちが望んでいるref
を取得できない以上、ref
で取得したものは何なのかよく考えてみましょう.Javascript言語のレベルに戻ってみると、それはコンポーネントのthisではないでしょうか.@withRouter
export default class childComponent extends Component {
constructor(props) {
super(props);
this.state = {}; // this
}
render() {
return (this is childComponent)
}
}
私たちが何を得る必要があるかを知っていれば、それはやりやすいです.私は
childComponent
にprop
を伝えて、このthisをgetすればいいのではないでしょうか.例えば、getInstance
を使っています.@withRouter
export default class childComponent extends Component {
constructor(props) {
super(props);
this.state = {};
const { getInstance } = props;
if (typeof getInstance === 'function') {
getInstance(this); // this `parentComponent`
}
}
render() {
return (this is childComponent)
}
}
@withRouter
export default class parentComponent extends Component {
constructor(props) {
super(props);
this.state = {};
}
render () {
return (
{ this.childCpWrapper = withRouter; }} // `withRouter` , ,
getInstance={(childCp) => { this.childCp = childCp; }} // `getInstance` `childComponent`
/>
);
}
}
perfect ! 問題は解決しました.このようにして、私はどのように高次コンポーネントを使っても、どのようにして私たちの
childComponent
を包んでも、私たちはgetInstance
を通じて、千山万水を通り抜けて直接childComponent
の例を得ることができます.もちろん完璧でも相対的であり、例えば上記のスキームでは、
childComponent
の構造関数ごとにthis
を暴露したコードを書かなければなりません.面倒で、苦労します.withRef
のようなHOCを書くことができます// , `WrappedComponent` `getInstance`( )
export default (WrappedComponent) => {
return class withRef extends Component {
static displayName = `withRef(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`;
render() {
// props :
// this.props.ref react ,
const props = {
...this.props,
};
// getInstance ref,
// `WrappedComponent`, getInstance `WrappedComponent`
// [yangshenghaha]
props.ref = (el)=>{
this.props.getInstance && this.props.getInstance(el);this.props.ref && this.props.ref(el);
}
return (
);
}
};
};
withRef
を使って@withRouter
@withRef // , : `childComponent`
export default class childComponent extends Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (this is childComponent)
}
}
@withRouter
export default class parentComponent extends Component {
constructor(props) {
super(props);
this.state = {};
}
render () {
return (
{ this.childCpWrapper = withRouter; }}
// `getInstance` `childComponent`
getInstance={(childCp) => { this.childCp = childCp; }}
/>
);
}
}
転載先:https://www.cnblogs.com/jcxfighting/p/10512005.html