ReactNative - FlatList renderItem道具

时间:2017-11-20 05:21:32

标签: android react-native

我的环境

  • "反应":" 16.0.0-beta.5"
  • " react-native":" 0.49.3"

您好。 我写了反应原生代码。

...
// ERROR

renderItem(item) {
  return (<ListItem item={item} />)
}

render() {
return (
  <View style={styles.container}>
    <FlatList
      data={realm.objects('Test')}
      keyExtractor={(item, index) => index}  
      renderItem={({item}) => this.renderItem(item)}
    />
  </View>
  );
}
...

但是,这段代码在android中没有用(在iOS中没问题)
所以,我尝试了很多不同的方式,我找到了作品代码! 只是直接使用FlatList的renderItem道具而不是通过resderItem()函数,就像这样。

...
// WORK

render() {
return (
  <View style={styles.container}>
    <FlatList
      data={realm.objects('Test')}
      keyExtractor={(item, index) => index}  
      renderItem={({item}) => <ListItem item={item} />}
    />
  </View>
  );
}
...

有什么不同?
请教我。

P.S。当我运行第一个代码时,这是来自android logcat的错误日志。

11-20 11:52:07.697 3324-3324/com.myapp E/NativeViewHierarchyManager: Unable to update properties for view tag 398
                                                                 com.facebook.react.uimanager.IllegalViewOperationException: ViewManager for tag 398 could not be found
                                                                     at com.facebook.react.uimanager.NativeViewHierarchyManager.resolveViewManager(NativeViewHierarchyManager.java:106)
                                                                     at com.facebook.react.uimanager.NativeViewHierarchyManager.updateProperties(NativeViewHierarchyManager.java:123)
                                                                     at com.facebook.react.uimanager.UIImplementation.synchronouslyUpdateViewOnUIThread(UIImplementation.java:248)
                                                                     at com.facebook.react.animated.PropsAnimatedNode.updateView(PropsAnimatedNode.java:69)
                                                                     at com.facebook.react.animated.NativeAnimatedNodesManager.updateNodes(NativeAnimatedNodesManager.java:516)
                                                                     at com.facebook.react.animated.NativeAnimatedNodesManager.runUpdates(NativeAnimatedNodesManager.java:427)
                                                                     at com.facebook.react.animated.NativeAnimatedModule$1.doFrameGuarded(NativeAnimatedModule.java:117)
                                                                     at com.facebook.react.uimanager.GuardedFrameCallback.doFrame(GuardedFrameCallback.java:31)
                                                                     at com.facebook.react.modules.core.ReactChoreographer$ReactChoreographerDispatcher.doFrame(ReactChoreographer.java:136)
                                                                     at com.facebook.react.modules.core.ChoreographerCompat$FrameCallback$1.doFrame(ChoreographerCompat.java:107)
                                                                     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:909)
                                                                     at android.view.Choreographer.doCallbacks(Choreographer.java:723)
                                                                     at android.view.Choreographer.doFrame(Choreographer.java:655)
                                                                     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
                                                                     at android.os.Handler.handleCallback(Handler.java:789)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                     at android.os.Looper.loop(Looper.java:164)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
11-20 11:52:07.761 3324-3351/com.myapp E/unknown:ReactNative: Exception in native call
                                                          java.lang.RuntimeException: Cannot add a child that doesn't have a YogaNode to a parent without a measure function! (Trying to add a 'ReactVirtualTextShadowNode' to a 'LayoutShadowNode')
                                                              at com.facebook.react.uimanager.ReactShadowNode.addChildAt(ReactShadowNode.java:181)
                                                              at com.facebook.react.uimanager.UIImplementation.setChildren(UIImplementation.java:398)
                                                              at com.facebook.react.uimanager.UIManagerModule.setChildren(UIManagerModule.java:310)
                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                              at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:363)
                                                              at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:162)
                                                              at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
                                                              at android.os.Handler.handleCallback(Handler.java:789)
                                                              at android.os.Handler.dispatchMessage(Handler.java:98)
                                                              at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
                                                              at android.os.Looper.loop(Looper.java:164)
                                                              at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:194)
                                                              at java.lang.Thread.run(Thread.java:764)
11-20 11:52:07.765 3324-3351/com.myapp E/unknown:ReactNative: Exception in native call
                                                          java.lang.RuntimeException: Cannot add a child that doesn't have a YogaNode to a parent without a measure function! (Trying to add a 'ReactVirtualTextShadowNode' to a 'LayoutShadowNode')
                                                              at com.facebook.react.uimanager.ReactShadowNode.addChildAt(ReactShadowNode.java:181)
                                                              at com.facebook.react.uimanager.UIImplementation.setChildren(UIImplementation.java:398)
                                                              at com.facebook.react.uimanager.UIManagerModule.setChildren(UIManagerModule.java:310)
                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                              at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:363)
                                                              at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:162)
                                                              at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
                                                              at android.os.Handler.handleCallback(Handler.java:789)
                                                              at android.os.Handler.dispatchMessage(Handler.java:98)
                                                              at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
                                                              at android.os.Looper.loop(Looper.java:164)
                                                              at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:194)
                                                              at java.lang.Thread.run(Thread.java:764)

1 个答案:

答案 0 :(得分:0)

您应该声明您的方法:

&#13;
&#13;
renderItem({ item }) {
  return (<ListItem item={item} />)
}
&#13;
&#13;
&#13;