简体   繁体   English

如何在React Native Expo中使用Realm?

[英]How to use realm in react native expo?

I want to use relamDb in my react native expo project. 我想在我的React Native Expo项目中使用relamDb。 I run the following command to install realm in my project- 我运行以下命令在我的项目中安装领域-

npm install --save realm npm install-保存领域

it doesn't show any error while installing. 安装时不显示任何错误。 After installing, in my project I have created two classes - App.js and TodoListComponent.js 安装后,在我的项目中,我创建了两个类-App.jsTodoListComponent.js

App.js App.js

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';

import TodoListComponent from './components/TodoListComponent';

export default class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <TodoListComponent/>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

TodoListComponent.js TodoListComponent.js

import React, { Component } from 'react';
import { View, FlatList, Text, TouchableOpacity, StyleSheet, Alert } from 'react-native';
import { updateTodoList, deleteTodoList, queryAllTodoLists } from '../databases/allSchemas';
import realm from '../databases/allSchemas';
import Swipeout from 'react-native-swipeout';

import HeaderComponent from './HeaderComponent';
import PopupDialogComponent from './PopupDialogComponent';
let FlatListItem = props => {
    const { itemIndex, id, name, creationDate, popupDialogComponent, onPressItem } = props;
    showEditModal = () => {
        popupDialogComponent.showDialogComponentForUpdate({
            id, name
        });
    }
    showDeleteConfirmation = () => {
        Alert.alert(
            'Delete',
            'Delete a todoList',
            [
                {
                    text: 'No', onPress: () => { },//Do nothing
                    style: 'cancel'
                },
                {
                    text: 'Yes', onPress: () => {
                        deleteTodoList(id).then().catch(error => {
                            alert(`Failed to delete todoList with id = ${id}, error=${error}`);
                        });
                    }
                },
            ],
            { cancelable: true }
        );
    };
    return (
        <Swipeout right={[
            {
                text: 'Edit',
                backgroundColor: 'rgb(81,134,237)',
                onPress: showEditModal
            },
            {
                text: 'Delete',
                backgroundColor: 'rgb(217, 80, 64)',
                onPress: showDeleteConfirmation
            }
        ]} autoClose={true}>
            <TouchableOpacity onPress={onPressItem}>
                <View style={{ backgroundColor: itemIndex % 2 == 0 ? 'powderblue' : 'skyblue' }}>
                    <Text style={{ fontWeight: 'bold', fontSize: 18, margin: 10 }}>{name}</Text>
                    <Text style={{ fontSize: 18, margin: 10 }} numberOfLines={2}>{creationDate.toLocaleString()}</Text>
                </View>
            </TouchableOpacity>
        </Swipeout >
    );
}
export default class TodoListComponent extends Component {
    constructor(props) {
        super(props);
        this.state = {
            todoLists: []
        };
        this.reloadData();
        realm.addListener('change', () => {
            this.reloadData();
        });
    }
    reloadData = () => {
        queryAllTodoLists().then((todoLists) => {
            this.setState({ todoLists });
        }).catch((error) => {
            this.setState({ todoLists: [] });
        });
        console.log(`reloadData`);
    }
    render() {
        return (<View style={styles.container}>
            <HeaderComponent title={"Todo List"}
                hasAddButton={true}
                hasDeleteAllButton={true}
                showAddTodoList={
                    () => {
                        this.refs.popupDialogComponent.showDialogComponentForAdd();
                    }
                }
            />
            <FlatList
                style={styles.flatList}
                data={this.state.todoLists}
                renderItem={({ item, index }) => <FlatListItem {...item} itemIndex={index}
                    popupDialogComponent={this.refs.popupDialogComponent}
                    onPressItem={() => {
                        alert(`You pressed item `);
                    }} />}
                keyExtractor={item => item.id}
            />
            <PopupDialogComponent ref={"popupDialogComponent"} />
        </View>);
    }
}
const styles = StyleSheet.create({
    container: {
        flex: 1,
        flexDirection: 'column',
        justifyContent: 'flex-start',
    },
    flatList: {
        flex: 1,
        flexDirection: 'column',
    }
});

After these coding, when I run the application it shows the following error- 经过这些编码后,当我运行应用程序时,它显示以下错误-

missing Realm constructor. 缺少Realm构造函数。 Did you run "react-native link realm" ? 您是否运行了“反应本地链接领域”? Please see https://realm.io/docs/react-native/latest/#missing-realm-constructor for troubleshooting 请参阅https://realm.io/docs/react-native/latest/#missing-realm-constructor进行故障排除

缺少Realm构造函数错误屏幕截图

I have tried to find out problem from the below links- 我试图从下面的链接中找出问题-

https://github.com/realm/realm-js/issues/1407 https://github.com/realm/realm-js/issues/1407

https://github.com/realm/realm-js/issues/1340 https://github.com/realm/realm-js/issues/1340

But none of these were helpful to me. 但是这些都不对我有帮助。 So, it would be very nice if some one helps me to know how to use realmDb in React native expo project. 因此,如果有人帮助我知道如何在React native expo项目中使用realmDb会非常好。

Expo does not support realm. 世博会不支持领域。

You will have to eject from expo and then start using realm 您将不得不从博览会中退出,然后开始使用领域

Please note that Expo does not support Realm, From the docs. 请注意,Expo不支持Realm,来自docs。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM