简体   繁体   English

如何从既不是子项也不是父项的组件调用函数?

[英]How do I call a function from component that is not a child nor parent?

Component A组分 A

const query = .... const 查询 = ....

getData(query);获取数据(查询);

Component B组分 B

//here I want to call it using component's A argument //这里我想使用组件的A参数来调用它

You would probably have a parent around these two components.您可能会在这两个组件周围有一个父级。 You can use the parent to share the function:您可以使用父级共享该功能:

function Parent() {
    const [ dataWithQuery, setDataWithQuery ] = useState(null);

    return (
        <>
            <ComponentA setDataWithQuery={ setDataWithQuery  } />
            <ComponentB dataWithQuery={ dataWithQuery } />
        </>
    );
}

and in Component A you'd have:在组件 A 中,您将拥有:

function ComponentA({ setDataWithQuery }) {
    const yourQueryHere = '...'

    function getData(query) { ... }

    useEffect(() => {
        setDataWithQuery(() => getData(yourQueryHere));
    }, []);

    ...
}

Notice the line where I setDataWithQuery(...), I created a new function that calls getData with your query variable.请注意我在 setDataWithQuery(...) 的行,我创建了一个新函数,该函数使用您的查询变量调用 getData。 This is how you save the function parameter to be used outside of ComponentA.这是您保存要在 ComponentA 之外使用的函数参数的方式。

and in Component B you'd have:在组件 B 中,您将拥有:

function ComponentB({ dataWithQuery }) {
    useEffect(() => {
        if(dataWithQuery != null) {
            const data = dataWithQuery();
        }
    }, [ dataWithQuery ]);


    ...
}

But there is no real reason to structure like this.但是没有真正的理由来构建这样的结构。 Why not pass up the query from ComponentA to a parent, get the data from the parent using that query, then pass that data to ComponentB?为什么不将查询从 ComponentA 传递给父级,使用该查询从父级获取数据,然后将该数据传递给 ComponentB?

Edit: you could also look up React Contexts for sharing without passing up and down parent/child.编辑:您还可以查找React Contexts进行共享,而无需上下传递父/子。 There would still need to be a parent around.仍然需要有父母在身边。

Do you mean, you want to pass a query down to ComponentA, get the data from within it, then use that data in ComponentB?您的意思是,您想将查询传递给 ComponentA,从中获取数据,然后在 ComponentB 中使用该数据?

If that is not what you want and you want to use the same query in each one, then just keep the query parameter in the component above them post and pass them down in props.如果这不是您想要的并且您想在每个查询中使用相同的查询,那么只需将查询参数保留在它们上面的组件中 post 并在 props 中传递它们。

If the former is what you want:如果前者是你想要的:

Parent Component:父组件:

import "./styles.css";
import ComponentA from "./ComponentA";
import ComponentB from "./ComponentB";
import { useState } from "react";

    export default function App() {
      const [query, setQuery] = useState("get some data");
      const [data, setData] = useState({});
      return (
        <div className="App">
          <ComponentA query={query} setData={setData} />
          <ComponentB data={data} />
        </div>
      );

Component A组分 A

import React, { useEffect } from "react";

function ComponentA({ query, setData }) {
  useEffect(() => {
    if (query !== "") {
      getData(query);
    }
  }, [query]);

  async function getData(query) {
    //do some stuff
    const result = { id: "1", message: "I am data" };
    setData(result);
  }

  return (
    <div>
      <h1>I am component A</h1>
    </div>
  );
}

export default ComponentA;

ComponentB组件B

function ComponentB({ data }) {

  function doSomethingWithComponentAData() {
    //do stuff with the data from componentA
  }
  return (
    <div>
      <h1>I am Component B, I got data: {data.id}</h1>
    </div>
  );
}

export default ComponentB;

Try this:试试这个:

// Component A

export const getData = () => { // ... }
// Component B

import { getData } from './ComponentA'

const result = getData()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM