Hey,
I use a custom hook build with useTracker and pass the query
-selector and the options
(to provide sorting functionality on table header click) as dependencies.
contactsHook.js
export const useContacts = (query = {}, options = {}) => useTracker(() => {
const subscriptionHandle = Meteor.subscribe('contacts', query, options);
const contacts = Contacts.find(query, options).fetch();
return {contacts, isLoading: !subscriptionHandle.ready()};
}, [query, options]);
contactList.jsx
export ContactList(props) => {
const [sortSelector, setSortSelector] = useState({});
const [sortState, setSortState] = useState({key: null, direction: null,});
const {contacts, isLoading} = useContacts({}, {sort: sortSelector});
if (isLoading) return <Loader/>;
const sort = (columnKey) => {
// sort direction logic depending on semantic ui react
const newDirection = sortState.key !== columnKey ? 'ascending' :
sortState.direction === 'ascending' ? 'descending' : 'ascending';
setSortState({key: columnKey, direction: newDirection});
setSortSelector(columnKey, newDirection === 'descending' ? 1 : -1);
};
return <Table>
<Table.Header>
<Table.HeaderCell
onClick={() => sort("firstName")}>
First Name
</Table.HeaderCell>
{/*...more headerCells and tableCode using contacts...*/}
</Table.Header>
{/*...tableCode using contacts...*/}
</Table>
}
That worked fine before the meteor update. After the update I get
react_devtools_backend.js:2430 Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.
→ The app runs into a loop of rerenders and crashes
When I remove the query
-selector and the options
and only pass a empty are as dependency to the hook, the warning is gone, but obviously the sorting doesn’t work anymore because a change of the sortSelector does not trigger a rerun of the useTracker hook anymore.
I wonder if this is a design mistake and meteor 2.0 is just more strict about that or if it’s a bug…
Some ideas about that? Thanks!
PS: Besides that I really really love meteor 2.0 and it’s hot module replacement…this makes the development so much more agile