How does React get an instance of a component?

the requirements are as follows:

multiple components call a method fn of A component to save the internal data of A component .
and this fn is dynamic and varies from component to component.

based on my past experience:

you can call and modify fn

as long as you get an instance of A component .

but don"t know how to get an instance of the component ? Or some other way?

Thank you ~

Mar.23,2021

ref
this.xxx=React.createRef ()

class A extends PureComponent{
        constructor(props){
            super(props);
            this.name=props.name;
        }
        show(){
            alert(this.name)
        }
        render(){
            return(
                    <div>1</div>
            )
        }
    }
    class App extends PureComponent{
        constructor(props){
            super(props);
            this.ref=React.createRef();
            this.show=this.show.bind(this);
        }
        show(){
            this.ref.current.show();
        }
        render(){
            return[
                <input type="button" value="show" onClick={this.show} />,
                <A name={'A'} ref={this.ref} />
            ]
        }
    }

something like this?


first of all, I don't know how your need came about.

multiple components call a method of component A fn , which can be solved according to component communication. It is not recommended to use this kind of "cool techs"

.

if it's just an academic question how to get an instance of a component

then it's very simple. One method that react provides is this.refs api

.

such as

  

feel that react and reudx do more logical items, which are very inflexible and difficult to use, and I don't know if it's my usage. The data flow is very chaotic and haematemesis

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b9d5d2-43909.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b9d5d2-43909.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?