简体   繁体   English

React.Children 不显示孩子

[英]React.Children does not display children

I want to use react-native-maps-clustering but this one only takes markers which are direct children of maps.我想使用react-native-maps-clustering但这个只需要标记,这些标记是地图的直接子级。

like the example像这个例子

<MapView>
  <Marker/>
  <Marker/>
</MapView>

In my case my markers are in another file nested in other elements在我的情况下,我的标记位于嵌套在其他元素中的另一个文件中

<MapView>
  <MarkersLayer/>
</MapView>

MarkersLayer.tsx MarkersLayer.tsx

<Fragment>
  <Trajectory/>
 <Fragment>
  <Marker>
     <MarkerStyle/>
  <Marker/>
 </Fragment>
</Fragment>

Test: In the library - ClusteredMapView.js - l.84测试:在库中 - ClusteredMapView.js - l.84

      propsChildren.forEach((child, index) => {
        //my test
        if (child.type?.name == "MarkersLayer"){
          console.log('I am MarkersLayer')
          React.Children.forEach(child, ch => {
            console.log("child MarkersLayer", ch)
          })
        }else if (child.props.coordinate){
          console.log("I am a marker", child)
        }
         //end
        if (isMarker(child)) {
          rawData.push(markerToGeoJSONFeature(child, index));
        } else {
          otherChildren.push(child);
        }
      });

Children in MapView: MapView 中的子项:

在此处输入图像描述

Children in MarkerLayer: MarkerLayer 中的子级:

在此处输入图像描述

Test: In my MarkerLayer component测试:在我的 MarkerLayer 组件中

  const propsChildren = useMemo(
    () => React.Children.toArray(children),
    [children],
  );

Result:结果:

在此处输入图像描述

The library uses React.Children to find the children contained in MapView but I can't get the markers in MarkerLayer.该库使用 React.Children 来查找 MapView 中包含的子项,但我无法在 MarkerLayer 中获取标记。

Thanks for the people reading this post and for the answers:)感谢阅读这篇文章的人们和答案:)

Try using useState() and useEffect() .尝试使用useState()useEffect()

This is done like so:这样做是这样的:

const [children, setChildren] = useState([]);

useEffect(() => {
    setChildren(React.Children.toArray(children))
}, []);

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

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