简体   繁体   English

从子组件执行父函数不会产生正确的结果:React + Typescript

[英]Executing parent function from child component does not give proper results : React+Typescript

I am trying to execute the parent function by calling it from child component and it does not yield proper results. 我试图通过从子组件中调用父函数来执行父函数,但它不会产生正确的结果。 There is a Pagination Component that computes the offsets for the pagination and sends the data to the child by calling parents method. 有一个分页组件,可以计算分页的偏移量,并通过调用parent方法将数据发送给子级。

Console log statement inside Parent gives wrong result where as inside child's setOffSet() gives proper result and when sent to parent component's method in the next line, It gives wrong result. 父级内部的控制台日志语句给出错误的结果,而子级内部的setOffSet()给出正确的结果,并在下一行发送给父级组件的方法时,给出错误的结果。

Code Sandbox: https://codesandbox.io/s/react-typescript-v91un 代码沙箱: https//codesandbox.io/s/react-typescript-v91un

setState needs a callback function but you're immediately calling the handlePagination function so you'd be passing in the return value of handlePagination. setState需要一个回调函数,但是您将立即调用handlePagination函数,因此您需要传递handlePagination的返回值。 Try this instead... 试试这个...

this.setState(
      { fromIndex },
      () => this.props.handlePagination(this.state.fromIndex, NO_OF_RECORDS_PER_PAGE)
    );

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

相关问题 如何从父组件调用 React/Typescript 子组件 function? - How to call React/Typescript child component function from parent component? 从父组件执行子(组件)功能 - Executing child (component) function from the parent component 从子组件调用的反应父函数不更新状态? - React Parent Function Called from Child Component does not Update State? 在 React 中从父组件到子组件调用 function - Calling function from parent to child component in React React 子组件不会更新从父组件传递的属性 - React Child Component does not update properties passed from Parent Component 在React.js中从父组件调用子组件函数 - Calling Child component function from Parent component in React.js 无法从 React 中的子组件调用父组件 function - Unable to call parent component function from child component in React 将 React-Typescript 中的 Function 道具从父级传递给子级 - Passing Function Props in React-Typescript from parent to child React Native子组件未从父组件接收更新状态 - React Native child component does not receive updated state from parent 正确的React-ful方式来处理父组件中只能作用于所选子组件的函数? - Proper React-ful way to handle a function in a parent component that will only act on selected child components?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM