简体   繁体   English

如何在reactjs中调用无状态功能组件内部的函数

[英]How can I call a function inside of stateless functional component in reactjs

Here is my problem I want to call function refresh from MyTable.js into MyPage.js这是我的问题,我想从 MyTable.js 调用函数刷新到 MyPage.js

MyTable.js MyTable.js

const MyTable = (props) => {

 //Some state

 const refresh = () => {//Do Refresh}
 
 return(<Table/>);
}

export default MyTable;

MyPage.js我的页面.js

const MyPage = (props) => {
 //Some state

 const handleRefresh = () => {
  //How to call refresh of MyTable here?
 }

 return(
  <Button onClick={handleRefresh} >Refresh</Button>
  <MyTable />
 )
}

//I Want a solution something like this but I don't know how to achieve //我想要一个类似这样的解决方案,但我不知道如何实现

const MyPage = (props) => {
 //Some state
const [myTable] = MyTable.useMyTable();

 const handleRefresh = () => {
  myTable.refresh();
 }

 return(
  <Button onClick={handleRefresh} >Refresh</Button>
  <MyTable table={myTable} />
 )
}

There is a way to do it but not highly recommended.有一种方法可以做到,但不强烈推荐。

You can do it like the following你可以像下面这样

MyTable.js MyTable.js

const MyTable = (props) => {

 props.refresh(() => {
    // DO Refresh
 });

 return(<Table/>);
}

export default MyTable;

MyPage.js我的页面.js

const MyPage = (props) => {
 //Some state

 let refreshFunc = () => {};

 const handleRefresh = () => {
  //How to call refresh of MyTable here?
  refreshFunc();
 }

 return(
  <Button onClick={handleRefresh} >Refresh</Button>
  <MyTable refresh={r => { refreshFunc = r; }} />
 )
}

Try to patch as follows:尝试打补丁如下:

const MyPage = (props) => {
 //Some state
 const [refresh, setRefresh] = useState(0);

 const handleRefresh = () => {
  setRefresh(x => x + 1);
 }

 return(
  <Button onClick={handleRefresh} >Refresh</Button>
  <MyTable dummy={refresh} />
 )
}

暂无
暂无

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

相关问题 在React上,如何在功能/无状态组件上调用组件安装上的函数? - On React, how can I call a function on component mount on a functional/stateless component? 我如何在功能组件中调用 function - ReactJs - How do i call function in functional component - ReactJs ReactJS class 组件和功能组件如何从普通 Javascript ZC1C425268E68385D1AB50741? - How do I call ReactJS class component and functional component from normal Javascript function? 如何将具有切换 function 的 state 组件转换为 reactjs 中的无状态组件? - How can I convert a state component that has a toggle function to a stateless one in reactjs? 我如何调用/执行外部功能组件的 function 反应? - How can i call/execute function of functional component outside react? Reactjs - 使用功能组件在应用程序启动时调用函数 - Reactjs - Call function on startup of application with a functional component 如何在 ReactJS 的功能组件中管理 4 Checkbox 的状态? - How I can manage the state for 4 Checkbox in functional component in ReactJS? 无状态组件内部的功能 - function inside stateless component 我们可以在不是功能组件的 function 中使用 redux 操作,也可以在 Z6F26340DBB728849B1903 中使用 class 组件 - Can we use redux actions inside a function that is not a functional component or a class component in reactJS 如何在子组件内部调用父组件的功能? - How can I call the function of a parent component inside of a child component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM