我正在编写一个轮询装饰器作为组件,该组件应该在React客户端中的定义间隔之后呈现所有子代。
const MyComponent = () => {
return <Polling seconds={10}>
<Query query={GET_NOTIFICATION_COUNTER>
{data: { counter } => {
...
return <Badge counter={counter}>
<Icon icon={notification} />
</Badge>
}}
</Query>
</Polling>
}
// https://overreacted.io/making-setinterval-declarative-with-react-hooks/
// https://reactjs.org/docs/hooks-custom.html
import useInterval from '@use-it/interval'
const Polling = ({ seconds = 1, children }) => {
useInterval(() => {
console.log('timer', children)
// how to trigger the rendering of the children
}, seconds * 1000)
return children
}
export default Polling
解决方案
我只是将pollInterval属性放在Apollo GraphQL <Query />
(Link to the Docs)上,然后删除我的<Polling />
组件。
<Query query={GET_NOTIFICATION_COUNTER pollInterval={300000}>
...
</Query>