4

Realm から返された結果をレンダリングする反応ネイティブな ListView を作成しようとしています。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 メソッドに受け入れられる必要があります。

誰かが私が間違っていること、またはこれを修正する方法を教えていただければ幸いです。

PS私は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>}
        />
      );
    }
}
4

2 に答える 2

0

render メソッドにあった原因を見つけました。

それ以外の:

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>}
    />
  );
}

はオブジェクトだったので、rowDataReactNative は Text 要素でレンダリングできませんでした

于 2016-08-10T17:30:18.293 に答える
0

を呼び出すときにこのエラーが発生していますcloneWithRowsか? その場合は、代わりに を使用するときにオブジェクトcloneWithRowsのスナップショットを呼び出す必要があります。これは、こちらの Realm の例で行われます。したがって、おそらく を使用して、コードを次のように変更してみてください。Realm.ResultsRealm.ListViewRealm.ListView

let pictures = [ realm.objects('picture').filtered('new == true').snapshot() ];
于 2016-07-31T23:53:15.380 に答える