Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React不能调用方法_Javascript_Reactjs - Fatal编程技术网

Javascript React不能调用方法

Javascript React不能调用方法,javascript,reactjs,Javascript,Reactjs,如何从不同的类调用方法。 我一直在寻找React入门,但所有示例都是关于渲染组件的。我只想设置一些变量 我试过这个。如何从类应用程序调用testMethod class App extends React.Component { constructor(props) { super(props); this.myRef = React.createRef(); } componentDidMount() {

如何从不同的类调用方法。 我一直在寻找React入门,但所有示例都是关于渲染组件的。我只想设置一些变量

我试过这个。如何从类应用程序调用testMethod

     class App extends React.Component {
      constructor(props) {
        super(props);
       this.myRef = React.createRef();
       }  

    componentDidMount() {
    this.myRef.current.testMethod();
    {

    }

render(){
return(
<TestClass ref={this.myRef} />
)
}
    export default App;

为了使用ref来访问组件方法,您需要呈现组件并将ref实例指定给它,如下所示

class App extends React.Component {
  constructor(props) {
    super(props);
    this.TestClass = React.createRef();
   }  

   componentDidMount() {
       this.TestClass.testMethod();
   }
   render() {
       return <TestClass ref={this.TestClass} />
   }
}
export default App;
要从App类内部调用testMethod(),必须将其作为道具传递给App组件:

class TestClass extends React.Component 
{
    testMethod = () =>{
        console.log("testMethod")
    }
render(){
return {
<App testMethod={testMethod} />  //How to access App?
}
}


 class App extends React.Component {
  constructor(props) {
    super(props);

   }  

componentDidMount() {
this.props.testMethod();
}
export default App;


}
class TestClass扩展了React.Component
{
testMethod=()=>{
log(“testMethod”)
}
render(){
返回{
//如何访问应用程序?
}
}
类应用程序扩展了React.Component{
建造师(道具){
超级(道具);
}  
componentDidMount(){
this.props.testMethod();
}
导出默认应用程序;
}

在HTML元素上使用ref属性时,使用React.createRef()在构造函数中创建的ref将底层DOM元素作为其当前属性接收


在自定义类组件上使用ref属性时,ref对象将组件的已装入实例作为其当前实例接收

因此: 在应用程序类文件中

import * as React from 'react';
import TestClass from './TestClass';
class App extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }
  componentDidMount() {
    this.myRef.current.testMethod();
  }
  render() {
     return <TestClass ref={this.myRef} />
  }
}
export default App;
import*as React from'React';
从“./TestClass”导入TestClass;
类应用程序扩展了React.Component{
建造师(道具){
超级(道具);
this.myRef=React.createRef();
}
componentDidMount(){
this.myRef.current.testMethod();
}
render(){
返回
}
}
导出默认应用程序;
在TestClass文件中:

import * as React from 'react';
class TestClass extends React.Component {
   testMethod = () => {
     console.log('test');
   }
   render() {
     return <div>Test</div>
   }
}
export default TestClass;
import*as React from'React';
类TestClass扩展了React.Component{
testMethod=()=>{
console.log('test');
}
render(){
回归试验
}
}
导出默认测试类;

Hi,看看您是否正在呈现testClass组件并将ref实例分配给it@ShubhamKhatri我不想渲染任何东西,只需调用method@Bobek,请查看我的回答您的组件必须实现render()方法,此错误与ref无关。请注意TestClass否'render'方法是否导出TestClass?如'export default TestClass'和in-App class,如果App和TestClass位于同一文件夹中,则需要从'./TestClass'导入TestClass
class TestClass extends React.Component 
{
    testMethod = () =>{
        console.log("testMethod")
    }
render(){
return {
<App testMethod={testMethod} />  //How to access App?
}
}


 class App extends React.Component {
  constructor(props) {
    super(props);

   }  

componentDidMount() {
this.props.testMethod();
}
export default App;


}
import * as React from 'react';
import TestClass from './TestClass';
class App extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }
  componentDidMount() {
    this.myRef.current.testMethod();
  }
  render() {
     return <TestClass ref={this.myRef} />
  }
}
export default App;
import * as React from 'react';
class TestClass extends React.Component {
   testMethod = () => {
     console.log('test');
   }
   render() {
     return <div>Test</div>
   }
}
export default TestClass;