question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update example apps to show Realm integrated with the latest React Native APIs (React Hooks, FlatList, ...)

See original GitHub issue

I created this in stackoverflow: https://stackoverflow.com/questions/55741282/flatlist-not-updating-with-react-hooks-and-realm

Basically, I created a React Hook to access Realm:

/* @flow */

import { useState, useEffect } from 'react';

export default function useRealmResultsHook(query, args): Array {
  const [data, setData] = useState(args ? query(...args) : query());

  useEffect(
    () => {
      function handleChange(newData) {
        setData(newData);
      }

      const dataQuery = args ? query(...args) : query();
      dataQuery.addListener(handleChange);
      return () => {
        dataQuery.removeAllListeners();
      };
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [query, ...args]
  );

  return data;
}

And then I use it:

const MyComponent = (props: Props) => {
  const data = useRealmResultsHook(getDataByType, [props.type]);

  return (
    <View>
      <FlatList
        data={data}
        keyExtractor={keyExtractor}
        renderItem={renderItem}
      />
    </View>
  );
};

Long story short, FlatList does not update because it looks like {{data === newData }} (reference) as Realm mutates the result (not creating a new copy).

Like this works:

setData([...newData]);

But doing so I get a plain JS array, not Realm Results anymore. What is the best way to do this? How to better clone Realm Results? I am afraid that [...newData] might be quite inefficient for big amounts of data.

TBH, it looks to be nothing about the hook but just Realm

  • FlatList (FlatList expects always a new array reference in order to update, not a mutated on).

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
ferrannpcommented, Jul 15, 2019

This is the workaround I ended up using:

function handleChange() {
  setData({
    data: newData,
    // Realm mutates the array instead of returning a new copy,
    // thus for a FlatList to update, we can use a timestamp as
    // extraData prop
    timestamp: Date.now(),
  });
}

And then in FlatList:

<FlatList
  ...
  extraData={timestamp}
/>

In this way, every time Realm updates, I force FlatList to update.

4reactions
zekcommented, Jul 3, 2019

I believe we also need a sample without hooks. It is not clear how to use realmjs with react-native clearly. Examples are deprecated they must be updated

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use Realm React - MongoDB
Realm React is an npm package that provides an easy-to-use API to perform common ... a set of hooks that update React state...
Read more >
Realm JavaScript for React Native applications - YouTube
In this event, Andrew Meyer, Software Engineer, on the Realm JavaScript team, will walk us through the React Native ecosystem as it relates ......
Read more >
FlatList not updating with React Hooks and Realm
FlatList is a PureComponent, so it needs an entirely new array reference as data for it to re-render. I'm not sure how Realm...
Read more >
Integration with Existing Apps - React Native
React Native is great when you are starting a new mobile app from scratch. However, it also works well for adding a single...
Read more >
Realtime Database - React Native Firebase
# Install & setup the app module yarn add @react-native-firebase/app # Install the database module yarn add @react-native-firebase/database # If you're ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found