[英]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.