Update example apps to show Realm integrated with the latest React Native APIs (React Hooks, FlatList, ...)
See original GitHub issueI 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:
- Created 4 years ago
- Comments:11 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

This is the workaround I ended up using:
And then in FlatList:
In this way, every time Realm updates, I force FlatList to update.
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