Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
e63cbd0
Implement a computationHandler API, with a cleanup method
CaptainN Jul 18, 2019
c9d2e2f
use a self contained forceUpdate value
CaptainN Jul 18, 2019
1315b55
Add a check and a warning for the appropriate return type from comput…
CaptainN Jul 19, 2019
70fab72
Merge branch 'hooks' into hooks-cleanup-2
CaptainN Jul 19, 2019
bb29e32
Rewrite readme for brevity and to update and explain optional `deps`
CaptainN Jul 19, 2019
574416a
Add some basic tests for useTracker
CaptainN Jul 19, 2019
76e6c2c
Merge branch 'hooks' into hooks-cleanup-2
CaptainN Jul 19, 2019
796ce6d
add some after unmount tests
CaptainN Jul 19, 2019
f0df78a
make sure we aren't accidentally triggering reactiveDict's migration …
CaptainN Jul 19, 2019
0c1dd3c
Test changes to enclosed values when not using deps too
CaptainN Jul 19, 2019
ca47cc5
Merge branch 'hooks' into hooks-cleanup-2
CaptainN Jul 20, 2019
68ec9c9
Allow setting deps in withTracker options
CaptainN Jul 20, 2019
a4ce87d
add missing typeof operator
CaptainN Jul 20, 2019
f44809f
Use the full hook on the server, to allow complete computation lifecycle
CaptainN Jul 20, 2019
85fb484
Add some argument checks and move the deps check to the same block
CaptainN Jul 20, 2019
9975c73
Add tests for the computation lifecycle
CaptainN Jul 20, 2019
7de2ea6
Fix forceUpdate by using a reference to update the counter value
CaptainN Jul 20, 2019
9e59a83
add a comment explaining the use of a reference with forceUpdate and …
CaptainN Jul 20, 2019
ef320e7
Merge branch 'hooks' into hooks-cleanup-2
CaptainN Jul 20, 2019
bff72c7
Use a much cleaner forceUpdate method based on useReducer
CaptainN Jul 21, 2019
b9d6911
Update to avoid running side effects in render, cause double invocati…
CaptainN Jul 22, 2019
7e71790
Revert "Update to avoid running side effects in render, cause double …
CaptainN Jul 22, 2019
5d73c1a
Make "falsy" deps check in computation consistent with other checks.
CaptainN Jul 22, 2019
044e163
Port the old outdated tests to useTracker (all tests are without deps)
CaptainN Jul 23, 2019
884246c
get more ported tests working
CaptainN Jul 23, 2019
4976a3b
add a version of one of the ported tests with deps
CaptainN Jul 23, 2019
774350c
Add withTracker tests
CaptainN Jul 23, 2019
c3c73f6
Fix some previous stall points with new knowledge from old tests
CaptainN Jul 23, 2019
ee06642
fix typo in computation deps compare
CaptainN Jul 23, 2019
11f66b4
Merge branch 'hooks' into hooks-cleanup-2
CaptainN Jul 23, 2019
a127f3c
only check if third argument is a function if it's not falsy
CaptainN Jul 23, 2019
ce0e52b
tracked does not return a value, so don't assign it
CaptainN Jul 23, 2019
d5a2d14
add back a computation test
CaptainN Jul 23, 2019
8118837
use es5 functions in package.js
CaptainN Jul 23, 2019
f6d9529
Merge branch 'hooks' into hooks-cleanup-2
CaptainN Jul 23, 2019
aa498b5
Move argument checks to avoid using them in production
CaptainN Jul 23, 2019
2e786d2
remove remove use of deps in withTracker. It's impractical to use the…
CaptainN Jul 24, 2019
3e29ac5
Clarify some comments and fix the deps check inside the computation.
CaptainN Jul 24, 2019
8afb742
Merge branch 'hooks' into hooks-cleanup-2
CaptainN Jul 24, 2019
f472e22
Only warn the user if the dep value is not undefined, null or an array.
CaptainN Jul 24, 2019
070484d
Merge branch 'hooks' into hooks-cleanup-2
CaptainN Jul 24, 2019
d90ac32
better checks/optimizations
CaptainN Jul 24, 2019
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
38 changes: 22 additions & 16 deletions packages/react-meteor-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,40 @@ This package provides two ways to use Tracker reactive data in your React compon
- a hook: `useTracker` (v2 only, requires React `^16.8`)
- a higher-order component (HOC): `withTracker` (v1 and v2).

The `useTracker` hook, introduced in version 2.0.0, is slightly more straightforward to use (lets you access reactive data sources directly within your componenent, rather than adding them from an external wrapper), and slightly more performant (avoids adding wrapper layers in the React tree). But, like all React hooks, it can only be used in function components, not in class components.
The `withTracker` HOC can be used with all components, function or class.
The `useTracker` hook, introduced in version 2.0.0, embraces the [benefits of hooks](https://reactjs.org/docs/hooks-faq.html). Like all React hooks, it can only be used in function components, not in class components.

It is not necessary to rewrite existing applications to use the `useTracker` hook instead of the existing `withTracker` HOC. But for new components, it is suggested to prefer the `useTracker` hook when dealing with function components.
The `withTracker` HOC can be used with all components, function or class based.

It is not necessary to rewrite existing applications to use the `useTracker` hook instead of the existing `withTracker` HOC.

#### `useTracker(reactiveFn, deps)` hook

You can use the `useTracker` hook to get the value of a Tracker reactive function in your (function) components. The reactive function will get re-run whenever its reactive inputs change, and the component will re-render with the new value.

Arguments:
- `reactiveFn`: a Tracker reactive function (with no parameters)
- `deps`: an array of "dependencies" of the reactive function, i.e. the list of values that, when changed, need to stop the current Tracker computation and start a new one - for example, the value of a prop used in a subscription or a Minimongo query; see example below. This array typically includes all variables from the outer scope "captured" in the closure passed as the 1st argument. This is very similar to how the `deps` argument for [React's built-in `useEffect`, `useCallback` or `useMemo` hooks](https://reactjs.org/docs/hooks-reference.html) work.
If omitted, the Tracker computation will be recreated on every call.
- `reactiveFn`: A Tracker reactive function (with no parameters).
- `deps`: An optional array of "dependencies" of the reactive function. This is very similar to how the `deps` argument for [React's built-in `useEffect`, `useCallback` or `useMemo` hooks](https://reactjs.org/docs/hooks-reference.html) work. If omitted, the Tracker computation will be recreated on every render (Note: `withTracker` has always done this). If provided, the computation will be retained, and reactive updates after the first run will run asynchronously from the react render cycle. This array typically includes all variables from the outer scope "captured" in the closure passed as the 1st argument. For example, the value of a prop used in a subscription or a Minimongo query; see example below.

```js
import { useTracker } from 'meteor/react-meteor-data';

// React function component.
function Foo({ listId }) {
// This computation uses no value from the outer scope,
// and thus does not needs to pass a 'deps' argument (same as passing []).
const currentUser = useTracker(() => Meteor.user());
// The following two computations both depend on the 'listId' prop,
// and thus need to specify it in the 'deps' argument,
// in order to subscribe to the expected 'todoList' subscription
// or fetch the expected Tasks when the 'listId' prop changes.
// and thus does not needs to pass a 'deps' argument.
// However, we can optimize the use of the computation
// by providing an empty deps array. With it, the
// computation will be retained instead of torn down and
// rebuilt on every render. useTracker will produce the
// same results either way.
const currentUser = useTracker(() => Meteor.user(), []);

// The following two computations both depend on the
// listId prop. When deps are specified, the computation
// will be retained.
const listLoading = useTracker(() => {
// Note that this subscription will get cleaned up when your component is unmounted.
// Note that this subscription will get cleaned up
// when your component is unmounted or deps change.
const handle = Meteor.subscribe('todoList', listId);
return !handle.ready();
}, [listId]);
Expand Down Expand Up @@ -119,9 +125,9 @@ For more information, see the [React article](http://guide.meteor.com/react.html
- `useTracker` hook + `withTracker` HOC
- Requires React `^16.8`.
- Implementation is compatible with the forthcoming "React Suspense" features.
- The `withTracker` HOC is strictly backwards-compatible with the one provided in v1.x, the major version number is only motivated by the bump of React version requirement.
Provided they use a compatible React version, existing Meteor apps leveraging the `withTracker` HOC can freely upgrade from v1.x to v2.x, and gain compatibility with future React versions.
- The `withTracker` HOC is strictly backwards-compatible with the one provided in v1.x, the major version number is only motivated by the bump of React version requirement. Provided a compatible React version, existing Meteor apps leveraging the `withTracker` HOC can freely upgrade from v1.x to v2.x, and gain compatibility with future React versions.
- The previously deprecated `createContainer` has been removed.

- `react-meteor-data` v1.x / v0.x :
- `withTracker` HOC (+ `createContainer`, kept for backwards compatibility with early v0.x releases)
- Requires React `^15.3` or `^16.0`.
Expand Down
1 change: 1 addition & 0 deletions packages/react-meteor-data/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global Meteor*/
import React from 'react';

if (Meteor.isDevelopment) {
Expand Down
132 changes: 132 additions & 0 deletions packages/react-meteor-data/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions packages/react-meteor-data/package.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global Package */

Package.describe({
name: 'react-meteor-data',
summary: 'React higher-order component for reactively tracking Meteor data',
Expand All @@ -13,3 +15,10 @@ Package.onUse(function (api) {

api.mainModule('index.js');
});

Package.onTest(function (api) {
api.use(['ecmascript', 'reactive-dict', 'reactive-var', 'tracker', 'tinytest', 'underscore', 'mongo']);
api.use('test-helpers');
api.use('react-meteor-data');
api.mainModule('tests.js');
});
9 changes: 9 additions & 0 deletions packages/react-meteor-data/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "react-meteor-data",
"dependencies": {
"react": "16.8.6",
"react-dom": "16.8.6",
"react-test-renderer": "16.8.6",
"@testing-library/react-hooks": "1.1.0"
}
}
2 changes: 2 additions & 0 deletions packages/react-meteor-data/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './useTracker.tests.js';
import './withTracker.tests.js';
Loading