英文:
How would I be able to access a method in a component from another component
问题
我有两个组件,分别是 Navbar 和 Sidebar,它们在 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 (
<div>
<p>This is a gibberish</p>
<p> onClick={show} </p>
</div>
);
}
I have this inside Sidebar.jsx
export default function Sidebar() {
const myMethod = () => {
alert("Hi!!! I am Collins");
};
return (
<div>
<p>I am feeling good</p>
</div>
);
}
Now in my App.jsx I have
import Sidebar from "./Sidebar";
import Navbar from "./Nabar";
function App() {
return (
<>
<Sidebar />
<Navbar myMethod={myMethod} />
</>
);
}
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
<myComponent propName={propValue}/>
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 (
<>
<Sidebar myMethod={myMethod} />
<Navbar myMethod={myMethod} />
</>
);
}
Now you can access it from Navbar
as you are already doing:
const Navbar = (props) => {
// ...
<p onClick={props.myMethod}</p>
}
You can remove myMethod
from Sidebar
since Sidebar
is now receiving the same function as props.myMethod
.
英文:
<myComponent propName={propValue}/>
The code above is calling <myComponent/>
component and passing propValue
as propName
to it.<br>
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.<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 = () => {
alert("Hi!!! I am Collins");;
};
return (
<>
<Sidebar myMethod={myMethod} />
<Navbar myMethod={myMethod} />
</>
);
}
Now you can access it from Navbar
as you are already doing:
const Navbar = (props) => {
//...
<p onClick={props.myMethod}</p>
}
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>
)
}
Navbar.js
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 './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>
)
}
Navbar.js
export default function Navbar(props){
const show = props.myMethod;
return(
<div>
<p>This is a gibberish</p>
<p onClick={show} </p>
</div>
)
}
答案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(
<>
<div>
<p>This is a gibberish</p>
<button onClick={show} >sideBar Button</button>
</div>
</>
)
}
this is 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>
)
}
this is app.jsx
import React from 'react';
import Sidebar from './components/Sidebar'
return (
<>
<Sidebar />
</>
)
}
export default App;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论