英文:
React: Calling a child component's function when a tab is clicked
问题
App.js
import MyComponent from './MyComponent';
import React from 'react';
import './App.css';
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs';
import 'react-tabs/style/react-tabs.css';
function App() {
function handleTabSelect(index, last) {
if (index === 1 && myComponentRef.current) {
myComponentRef.current.funcA();
}
}
const myComponentRef = React.useRef();
return (
<div className="App">
<Tabs onSelect={handleTabSelect}>
<TabList>
<Tab>Tab 1</Tab>
<Tab>Tab 2</Tab>
</TabList>
<TabPanel>
HelloWorld123
</TabPanel>
<TabPanel>
<MyComponent ref={myComponentRef} />
</TabPanel>
</Tabs>
</div>
);
}
export default App;
MyComponent.jsx as a child component.
import React from 'react';
const MyComponent = React.forwardRef((props, ref) => {
function funcA() {
console.log('Function A is called');
myRef.current && myRef.current.focus();
}
// Save the ref to the DOM node you want to reference
const myRef = React.useRef(null);
React.useImperativeHandle(ref, () => ({
funcA: funcA
}));
return (
<div>
<p ref={myRef}>This is a message for Tab 2</p>
</div>
);
});
export default MyComponent;
不包括代码的翻译部分已经提供。
英文:
I am trying to create a React app that has two tabs(Tab1, Tab2) and when you click on Tab2, a message saying 'Function A is called' will appear in the console.
But it does not work with the following code.
The tabs works fine but no message on the console.
How can I solve it?
App.js
import MyComponent from './MyComponent';
import React from 'react';
import './App.css';
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs';
import 'react-tabs/style/react-tabs.css';
function App() {
function handleTabSelect(index, last) {
if (index === 1 && myComponentRef.current) {
myComponentRef.current.funcA();
}
}
const myComponentRef = React.useRef();
return (
<div className="App">
<Tabs onSelect={handleTabSelect}>
<TabList>
<Tab>Tab 1</Tab>
<Tab>Tab 2</Tab>
</TabList>
<TabPanel>
HelloWorld123
</TabPanel>
<TabPanel>
<MyComponent ref={myComponentRef} />
</TabPanel>
</Tabs>
</div>
);
}
export default App;
MyComponent.jsx as a child component.
import React from 'react';
const MyComponent = React.forwardRef((props, ref) => {
function funcA() {
console.log('Function A is called');
myRef.current && myRef.current.focus();
}
// Save the ref to the DOM node you want to reference
const myRef = React.useRef(null);
React.useImperativeHandle(ref, () => ({
funcA: funcA
}));
return (
<div>
<p ref={myRef}>This is a message for Tab 2</p>
</div>
);
});
export default MyComponent;
答案1
得分: 1
因为条件渲染,TabPanel-2 无法将任何内容转发到父组件。请参阅这里:https://beta.reactjs.org/reference/react/forwardRef#my-component-is-wrapped-in-forwardref-but-the-ref-to-it-is-always-null
至少有两种方法可以调用 funcA。
- 通过在 handleTabSelect 中使用 setTimeout 调用 funcA;当 TabPanel 在此渲染中显示时,setTimeout 将获取当前目标而不是 null。
setTimeout(() => {
if (index === 1 && myComponentRef.current) {
myComponentRef.current.funcA();
}
}, 0);
- 将一个 prop 发送到 TabPanel-2 并使用 Effect 监视并调用 funcA。
const [index, setIndex] = useState(0)
function handleTabSelect(index, last) {
setIndex(index)
}
<MyComponent index={index} />
useEffect(() => {
if(props.index===1){
funcA();
}
}, [props.index])
英文:
Because of conditional render, TabPanel-2 can't forward anything to parent component. see here. https://beta.reactjs.org/reference/react/forwardRef#my-component-is-wrapped-in-forwardref-but-the-ref-to-it-is-always-null
At least two ways to call funcA.
- call funcA by setTimeout in handleTabSelect;While TabPanel Has render to display in this render ,setTimeout will get the current target instead of null.
setTimeout(() => {
if (index === 1 && myComponentRef.current) {
myComponentRef.current.funcA();
}
}, 0);
- send a prop to TabPanel-2 and use Effect to watch and call funcA.
const [index, setIndex] = useState(0)
function handleTabSelect(index, last) {
setIndex(index)
}
<MyComponent index={index} />
useEffect(() => {
if(props.index===1){
funcA();
}
}, [props.index])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论