Skip to content

Provide useTracker function #17

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Meteor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Collection } from './Collection';
import call from './Call';

import withTracker from './components/ReactMeteorData';
import useTracker from './components/useTracker';

import ReactiveDict from './ReactiveDict';

Expand All @@ -29,6 +30,7 @@ module.exports = {
return new Collection(name, options);
},
withTracker,
useTracker,
getData() {
return Data;
},
Expand Down
23 changes: 23 additions & 0 deletions src/components/useTracker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React, { useEffect, useState } from 'react';
import Trackr from 'trackr';

export default useTracker = (trackerFn, deps = []) => {
const [response, setResponse] = useState(trackerFn());
let computation = null;

const stopComputation = () => {
computation && computation.stop();
computation = null;
};

useEffect(() => {
stopComputation();
Trackr.autorun(currentComputation => {
computation = currentComputation;
setResponse(trackerFn());
});
return stopComputation;
}, deps);

return response;
};