如何能够从另一个组件中访问一个组件中的方法

huangapple go评论68阅读模式
英文:

How would I be able to access a method in a component from another component

问题

我有两个组件,分别是 NavbarSidebar,它们在 App.jsx 中被渲染。我需要在 Navbar 中访问 Sidebar 内定义的一个方法。这是代码:

在 Navbar.jsx 中,我有以下内容:

export default function Navbar(props) {
  const show = props.myMethod;

  return (
    <div>
      <p>这是一些胡言乱语</p>
      <p onClick={show}>点击这里</p>
    </div>
  );
}

在 Sidebar.jsx 中,我有以下内容:

export default function Sidebar() {
  const myMethod = () => {
    alert("嗨!我是Collins");
  };

  return (
    <div>
      <p>我感觉很好</p>
    </div>
  );
}

现在,在我的 App.jsx 中,我有:

import Sidebar from "./Sidebar";
import Navbar from "./Navbar";

function App() {
  return (
    <>
      <Sidebar />

      <Navbar myMethod={myMethod} />
    </>
  );
}

我尝试将 props 传递给 Navbar,以便在需要的地方使用该方法,但出现了一个错误,提示 myMethod 未定义。

我知道这可能是一个简单的问题,但我刚开始自学 React。感谢您提前的帮助!我尝试将 props 传递给 Navbar,以便在需要的地方使用该方法,但出现了一个错误,提示 myMethod 未定义。我确保在 App.jsx 中导入了必要的内容,但错误仍然存在。

英文:

I have two component say Navbar and Sidebar which is rendered in App.jsx. I need a method defined inside Sidebar to be accessed in Navbar. This is the code

I have this inside Navbar.jsx

export default function Navbar(props) {
  const show = props.myMethod;

  return (
    &lt;div&gt;
      &lt;p&gt;This is a gibberish&lt;/p&gt;
      &lt;p&gt; onClick={show} &lt;/p&gt;
    &lt;/div&gt;
  );
}

I have this inside Sidebar.jsx

export default function Sidebar() {
  const myMethod = () =&gt; {
    alert(&quot;Hi!!! I am Collins&quot;);
  };

  return (
    &lt;div&gt;
      &lt;p&gt;I am feeling good&lt;/p&gt;
    &lt;/div&gt;
  );
}

Now in my App.jsx I have

import Sidebar from &quot;./Sidebar&quot;;
import Navbar from &quot;./Nabar&quot;;

function App() {
  return (
    &lt;&gt;
      &lt;Sidebar /&gt;

      &lt;Navbar myMethod={myMethod} /&gt;
    &lt;/&gt;
  );
}

I tried to pass props to the Navbar where I need the method but it comes out with an error that myMethod is not defined.

I know this is going to be a simple thing but I just started learning react by self teaching.

Thanks in advance!!!

I tried to pass props to the Navbar where I need the method but it comes out with an error that myMethod is not defined. I ensured the necessary things were imported in App.jsx but still the same error

答案1

得分: 1

&lt;myComponent propName={propValue}/&gt;

The code above is calling the myComponent component and passing propValue as propName to it. So propValue should exist in the component calling myComponent so that it can be passed as props.

In your code, you are trying to pass myMethod as myMethod to Navbar from App, but there is no myMethod defined there.

You can have a look at the React useContext hook. React Context is a way to manage state globally. It can be used together with the useState Hook to share state between deeply nested components more easily than with useState alone.

Or just declare myMethod in the App component, then pass it to Sidebar and Navbar:

function App() {
  const myMethod = () => {
    alert("Hi!!! I am Collins");
  };
  return (
    &lt;&gt;
      &lt;Sidebar myMethod={myMethod} /&gt;
      &lt;Navbar myMethod={myMethod} /&gt;
    &lt;/&gt;
  );
}

Now you can access it from Navbar as you are already doing:

const Navbar = (props) => {
  // ...
  &lt;p onClick={props.myMethod}&lt;/p&gt;
}

You can remove myMethod from Sidebar since Sidebar is now receiving the same function as props.myMethod.

英文:
&lt;myComponent propName={propValue}/&gt;

The code above is calling &lt;myComponent/&gt; component and passing propValue as propName to it.<br>
so propValue should exist in the component calling &lt;myComponent/&gt; so that it can be passed as props.

In your code you are trying to pass myMethod as myMethod to &lt;Navbar/&gt; from App but there is no myMethod defined there.<br>

You can have a look at React useContext hook<br>
>React Context is a way to manage state globally.<br>
It can be used together with the useState Hook to share state between deeply nested components more easily than with useState alone.

or just declare myMethod in the App component then pass it to SideBar and Navbar

function App() {
  const myMethod = () =&gt; {
    alert(&quot;Hi!!! I am Collins&quot;);;
  };
  return (
    &lt;&gt;
      &lt;Sidebar myMethod={myMethod} /&gt;
      &lt;Navbar myMethod={myMethod} /&gt;
    &lt;/&gt;
  );
}

Now you can access it from Navbar as you are already doing:

const Navbar = (props) =&gt; {
 //...
&lt;p onClick={props.myMethod}&lt;/p&gt;
}

You can remove myMethod from Sidebar since Sidebar is now receiving the same function as props.myMethod.

答案2

得分: 0

最简单的解决方案是在 App.js 中声明方法,然后将它传递给它的子组件(兄弟组件)。

以下是代码部分,不需要翻译:

App.js

import Sidebar from './Sidebar'
import Navbar from './Navbar'

function App(){

   const myMethod = () => {
       alert("Hi!!! I am Collins");
   }

   return (
       <>
           <Sidebar myMethod={myMethod}/>
           <Navbar myMethod={myMethod} />
       </>
    )
}

Sidebar.js

export default function Sidebar({ myMethod }){
    return(
        <div>
        <p>I am feeling good</p>
    </div>
   )
}
export default function Navbar(props){
    const show = props.myMethod;
    return(
        <div>
            <p>This is a gibberish</p>
            <p onClick={show}>Click me</p>
        </div>
    )
}

希望这有所帮助。

英文:

The most simple solution would be to hold the method declaration in App.js and then, pass it to it's child components (sibling) components.

Here is the code -

App.js

import Sidebar from &#39;./Sidebar&#39;
import Navbar from &#39;./Navbar&#39;

function App(){

   const myMethod = ()=&gt;{
       alert(&quot;Hi!!! I am Collins&quot;);
   }

   return (
       &lt;&gt;
           &lt;Sidebar myMethod={myMethod}/&gt;
           &lt;Navbar myMethod={myMethod} /&gt;
       &lt;/&gt;
    )
}

Sidebar.js

export default function Sidebar({ myMethod }){
    return(
        &lt;div&gt;
        &lt;p&gt;I am feeling good&lt;/p&gt;
    &lt;/div&gt;
   )
}
export default function Navbar(props){
    const show = props.myMethod;
    return(
        &lt;div&gt;
            &lt;p&gt;This is a gibberish&lt;/p&gt;
            &lt;p onClick={show} &lt;/p&gt;
        &lt;/div&gt;
    )
}

答案3

得分: 0

You should call Navbar component from Sidebar component.

这是NavBar.jsx

export default function Navbar(props) {

  const show = props.myMethod;
  
  return (
    <>
      <div>
        <p>This is a gibberish</p>
        <button onClick={show}>sideBar Button</button>
      </div>
    </>
  )
}

这是Sidebar.jsx

import Navbar from "./Navbar";
export default function Sidebar() {

  const myMethod = () => {
    alert("Hi!!! I am Collins");
  }
  
  return (
    <div>
      <p>I am feeling good</p>
      <Navbar myMethod={myMethod} />
    </div>
  )
}

这是app.jsx

import React from 'react';
import Sidebar from './components/Sidebar';

return (
  <>
    <Sidebar />
  </>
)

export default App;

英文:
You should call Navbar component from Sidebar component.

this is NavBar.jsx

export default function Navbar(props){

    const show = props.myMethod;
    
return(
    &lt;&gt;
    &lt;div&gt;
    &lt;p&gt;This is a gibberish&lt;/p&gt;
    &lt;button onClick={show} &gt;sideBar Button&lt;/button&gt;
    &lt;/div&gt;
    &lt;/&gt;
    )
    }

this is Sidebar.jsx

import Navbar from &quot;./Navbar&quot;;
export default function Sidebar(){

    const myMethod = ()=&gt;{
    
    alert(&quot;Hi!!! I am Collins&quot;);
    
    }
    
    return(
    
    &lt;div&gt;
    
    &lt;p&gt;I am feeling good&lt;/p&gt;
    &lt;Navbar myMethod={myMethod} /&gt;
    &lt;/div&gt;
    )
    
    }

this is app.jsx

import React from &#39;react&#39;;
import Sidebar from &#39;./components/Sidebar&#39;


return (
  &lt;&gt;
  &lt;Sidebar /&gt;
  
  &lt;/&gt;
  )
}


export default App;

huangapple
  • 本文由 发表于 2023年4月4日 13:56:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定