Skip to content

useTracker returns undefined for the second render #327

@afrokick

Description

@afrokick

The issue occurred in 2.2.2.
The issue not exists in 2.1.4.

@CaptainN After #306 the function starts to return undefined value.

I had some investigation.

It appears after second render. Because useMemo only fires on first render, the value from useMemo didn't store on next render.

https://github.com/meteor/react-packages/blob/master/packages/react-meteor-data/useTracker.ts#L117

I found discussion in PR about this line: #306 (comment)

So, we need a test for my test case?)

Simple component for reproducing:

import React, { useState } from 'react';
import { useTracker } from 'meteor/react-meteor-data';

export const App = () => {
  const [s, setS] = useState(false);
  useTracker(() => {
    //to trigger rerender
    setS(true);
  }, []);

  const links = useTracker(() => [], []);

  //BUG, because the 'links' must always be 'array'
  if (links === undefined) {
    console.error(`links === undefined`);//for 2d render it equals undefined
  }

  return (
    <div>
      <h2>Links count:{links.length}</h2>
    </div>
  );
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions