Reactjs 如何从react中另一个组件中的组件导出或调用方法?

Reactjs 如何从react中另一个组件中的组件导出或调用方法?,reactjs,Reactjs,我是新来的。也许有人能帮我。我有两个组件。Weather组件中的getData()应该从组件PointsOfInterest调用方法getPoint()。我该怎么做 构成部分1: import React from 'react'; import { getPoint, PointsOfInterest } from './PointsOfInterest' class Weather extends React.Component { getWeather = async

我是新来的。也许有人能帮我。我有两个组件。Weather组件中的getData()应该从组件PointsOfInterest调用方法getPoint()。我该怎么做

构成部分1:

import React from 'react';
import  { getPoint, PointsOfInterest } from './PointsOfInterest'

class Weather extends React.Component {
   
    getWeather = async (city) => {

  ...
        }

    }

 getData = async (city) => {
      await this.getWeather(city)
      getPoint(this.state.latitude, this.state.longitude); 
    }
render()
{
return ...    }

构成部分2:

export class PointsOfInterest extends React.Component {
state = {nameOfPoints: ''}
fetchToken = async () => {
...
}

getPoint = async (latitude, longitude) => {
  const auth = await this.fetchToken()
...

}
render(){
  return(...)
}
}

export default PointsOfInterest;


在天气组件中导入PointsOfInterest时,可以在PointsOfInterest中添加一个额外的prop ref(如下所示)。通过ref,您可以访问天气组件中的PointsOfInterest方法。我已经为天气组件做了下面的代码更改

你可以像我一样

import React from 'react';
import  { getPoint, PointsOfInterest } from './PointsOfInterest'

class Weather extends React.Component {

constuctor(props){
// Changes made here
this.pointOfIntrst =  React.createRef();

}
   
    getWeather = async (city) => {

  ...
        }

    }

 getData = async (city) => {
      await this.getWeather(city)
   if(this.pointOfIntrst.current) {
          await this.pointOfIntrst.current.getPoint(this.state.latitude, 
       this.state.longitude); 
}
    }
render()
{
<PointsOfInterest ref = {this.pointOfIntrst} .../>
return ...    }
从“React”导入React;
从“/PointsOfInterest”导入{getPoint,PointsOfInterest}
类。组件{
建筑师(道具){
//此处所做的更改
this.pointOfIntrst=React.createRef();
}
getWeather=async(城市)=>{
...
}
}
getData=async(城市)=>{
等待此消息。getWeather(城市)
if(当前的此点输入){
等待此.pointOfIntr.current.getPoint(this.state.latitude,
这个。州。经度);
}
}
render()
{
返回…}

在天气组件中导入兴趣点时,可以在兴趣点中添加一个额外的道具参考(如下所示)。通过ref,您可以访问天气组件中的PointsOfInterest方法。我已经为天气组件做了下面的代码更改

你可以像我一样

import React from 'react';
import  { getPoint, PointsOfInterest } from './PointsOfInterest'

class Weather extends React.Component {

constuctor(props){
// Changes made here
this.pointOfIntrst =  React.createRef();

}
   
    getWeather = async (city) => {

  ...
        }

    }

 getData = async (city) => {
      await this.getWeather(city)
   if(this.pointOfIntrst.current) {
          await this.pointOfIntrst.current.getPoint(this.state.latitude, 
       this.state.longitude); 
}
    }
render()
{
<PointsOfInterest ref = {this.pointOfIntrst} .../>
return ...    }
从“React”导入React;
从“/PointsOfInterest”导入{getPoint,PointsOfInterest}
类。组件{
建筑师(道具){
//此处所做的更改
this.pointOfIntrst=React.createRef();
}
getWeather=async(城市)=>{
...
}
}
getData=async(城市)=>{
等待此消息。getWeather(城市)
if(当前的此点输入){
等待此.pointOfIntr.current.getPoint(this.state.latitude,
这个。州。经度);
}
}
render()
{
返回…}

谢谢您的帮助。现在我有一个错误“未处理的拒绝(TypeError):无法读取未定义的属性'current'”,您还需要在构造函数中设置值,并且可以检查这些值。我已经编辑了答案对不起,构造器这个词拼错了。但是现在有另一个错误“未处理的拒绝(TypeError):this.pointOfIntrst.current.getPoint不是函数”,您可以尝试打印console.log(this.pointOfIntrst.current),并查看值。可能是您犯了一些错误,所以“this.pointofitr.current”有什么价值?谢谢您的帮助。现在我有一个错误“未处理的拒绝(TypeError):无法读取未定义的属性'current'”,您还需要在构造函数中设置值,并且可以检查这些值。我已经编辑了答案对不起,构造器这个词拼错了。但是现在有另一个错误“未处理的拒绝(TypeError):this.pointOfIntrst.current.getPoint不是函数”,您可以尝试打印console.log(this.pointOfIntrst.current),并查看值。可能是您犯了一些错误,“this.pointofitr.current”的值是多少