Docs Menu

Docs HomeDevelop ApplicationsAtlas Device SDK

Mixed - React Native SDK

On this page

  • Realm Object Models
  • Create an Object With a Mixed Value
  • Query for Objects with a Mixed Value
  • Mixed Properties and Type Checking

New in version realm@10.5.0.

The Mixed data type is a realm property type that can hold any valid Realm data type except a collection. You can create collections (lists, sets, and dictionaries) of type mixed, but a mixed type itself cannot be a collection.

The Mixed type is indexable, but you can't use it as a primary key.

Properties using the Mixed type can hold null values and cannot be defined as optional. All instances of the JavaScript Number type in a Realm Mixed type are mapped to the Realm double type.

To set a property of your object model as Mixed, set the property's type to mixed.

Create an object with a Mixed value by using the new operator within a write transaction.

In the following CreateCatsInput example, we create several Cat realm objects with a Mixed type for the birthDate field.

The CreateCatsInput component does the following:

  • Get access to the opened realm instance by calling the useRealm() hook.

  • Use React's useEffect hook
    to call an anonymous function only once with useEffect and an empty dependency array.
  • Within the anonymous function, we create four different Cat objects by
    using the new operator to create a new realm object within a write transaction. Each of the Cat objects uses a different data type for the birthDate property.
  • Use the useQuery() hook to retrieve all Cat objects.

  • Map through the cats to render
    a list of Text components displaying each cat's name and birthDate.

To query for objects with a Mixed value, run the Collection.filtered() method and pass in a filter for a non-Mixed field. You can then print the value of the Mixed property or the entire object itself.

In the following CatInfoCard example, we query for a Cat object using the cat's name.

The CatInfoCard component does the following:

  • Get all Cat objects by passing the Cat class to the useQuery() hook, and then use filtered() to filter the results to receive only the cats whose names match the name passed as a prop. We then get the first matching cat and store it as a const variable.

  • Use dot notation to retrieve the Mixed property, birthDate.

  • Display the cat's name and birthdate in the render method if Realm finds the cat. If there is no cat that matches the name passed into the component as a prop, we render text that says "Cat not found".

Because Mixed properties can be more than one type, you can't rely on the property's value being a specific type.

With Object.getPropertyType(), you can get a Mixed property's underlying type. This allows you build your own type checking.

// Use Type Predicates and Object.getPropertyType() to
// create a runtime type check for Mixed properties.
const isString = (
val: Mixed,
name: string,
object: Realm.Object,
): val is Realm.Types.String => {
return object.getPropertyType(name) === 'string';
};
type CatInfoCardProps = {catName: string};
const CatInfoCard = ({catName}: CatInfoCardProps) => {
const cat = useQuery(
Cat,
cats => {
return cats.filtered(`name = '${catName}'`);
},
[catName],
)[0];
// Use the type check to handle your data.
const catBirthDate = isString(cat.birthDate, 'birthDate', cat)
? cat.birthDate
: cat.birthDate.toString();
if (cat) {
return (
<>
<Text>{catName}</Text>
<Text>{catBirthDate}</Text>
</>
);
} else {
return <Text>Cat not found</Text>;
}
};
←  Sets - React Native SDKUUID - React Native SDK →