使用领域结果反应本机 ListView
React Native ListView with Realm Results
我正在尝试创建一个 react-native ListView 来呈现 Realm 返回的结果。我一直在按照我发现的有关 react-native 的 ListView 以及如何使用 Realm 的说明进行操作。
但是我总是得到同样的错误:Objects are not valid as a React child (found: [object Results]). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of 'Text'.
根据我对 Realm 文档和其他文章的理解,Results 对象的行为应该类似于 javascript 列表,因此应该被接受到 cloneWithRows 方法中。
如果有人能告诉我我做错了什么或如何解决这个问题,我们将不胜感激。
P.S。我已经尝试过 react-native 的 ListView 和 Realm 的 ListView,两者的行为方式相同。
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text,
Navigator,
TouchableHighlight,
TouchableOpacity,
//ListView,
} from 'react-native';
import { ListView } from 'realm/react-native';
import realm from './myrealm'
class contextview extends Component {
getState() {
console.log("getInitialState");
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
let pictures = [ realm.objects('picture').filtered('new == true') ];
console.log("pictures: " + pictures);
return {
//dataSource: ds.cloneWithRows(['row 1', 'row 2', 'row 3']),
dataSource: ds.cloneWithRows(pictures)
};
}
constructor(props)
{
super(props);
this.state = this.getState();
this.bindMethods();
}
render() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>{rowData}</Text>}
/>
);
}
}
您在调用 cloneWithRows
时遇到此错误吗?如果是这样,您可能需要在使用 Realm.ListView
时在 Realm.Results
对象的快照上调用 cloneWithRows
。这是在 Realm 示例 here 中完成的。所以也许尝试使用 Realm.ListView
并将代码更改为:
let pictures = [ realm.objects('picture').filtered('new == true').snapshot() ];
我在我的渲染方法中找到了原因,
而不是:
render() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>{rowData}</Text>}
/>
);
}
我应该做的:
render() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>{rowData.path}</Text>}
/>
);
}
由于 rowData
是一个对象,ReactNative 无法在文本元素中呈现它
我正在尝试创建一个 react-native ListView 来呈现 Realm 返回的结果。我一直在按照我发现的有关 react-native 的 ListView 以及如何使用 Realm 的说明进行操作。
但是我总是得到同样的错误:Objects are not valid as a React child (found: [object Results]). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of 'Text'.
根据我对 Realm 文档和其他文章的理解,Results 对象的行为应该类似于 javascript 列表,因此应该被接受到 cloneWithRows 方法中。
如果有人能告诉我我做错了什么或如何解决这个问题,我们将不胜感激。
P.S。我已经尝试过 react-native 的 ListView 和 Realm 的 ListView,两者的行为方式相同。
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text,
Navigator,
TouchableHighlight,
TouchableOpacity,
//ListView,
} from 'react-native';
import { ListView } from 'realm/react-native';
import realm from './myrealm'
class contextview extends Component {
getState() {
console.log("getInitialState");
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
let pictures = [ realm.objects('picture').filtered('new == true') ];
console.log("pictures: " + pictures);
return {
//dataSource: ds.cloneWithRows(['row 1', 'row 2', 'row 3']),
dataSource: ds.cloneWithRows(pictures)
};
}
constructor(props)
{
super(props);
this.state = this.getState();
this.bindMethods();
}
render() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>{rowData}</Text>}
/>
);
}
}
您在调用 cloneWithRows
时遇到此错误吗?如果是这样,您可能需要在使用 Realm.ListView
时在 Realm.Results
对象的快照上调用 cloneWithRows
。这是在 Realm 示例 here 中完成的。所以也许尝试使用 Realm.ListView
并将代码更改为:
let pictures = [ realm.objects('picture').filtered('new == true').snapshot() ];
我在我的渲染方法中找到了原因,
而不是:
render() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>{rowData}</Text>}
/>
);
}
我应该做的:
render() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>{rowData.path}</Text>}
/>
);
}
由于 rowData
是一个对象,ReactNative 无法在文本元素中呈现它