Skip to content

map的clone会导致重复render #346

Closed
@starInEcust

Description

@starInEcust

下面map这个代码

  {AMap &&
          map &&
          childs.map((child, key) => {
            if (!isValidElement(child)) return null;
            if (typeof child === 'string') {
              return cloneElement(<Fragment>{child}</Fragment>, { key });
            }
            if (child.type && typeof child.type === 'string') {
              return cloneElement(child, { key });
            }
            return cloneElement(child, {
              ...child.props,
              AMap,
              map,
              container,
              key,
            });
          })}

会导致子组件useMemo失效. 只要有state变化就会重复渲染,性能大大的坏。
比如下面代码, 任意的state变化,都会导致Circle的重新渲染,非常的神奇。
我就是在切换数据源的时候发现Circle会多次渲染,还没有销毁,然后发现了Circle销毁逻辑有问题

const circle = useMemo(() => <Circle />, []);
return 
<Map><Circle /> <Map>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions