Mar*_*ahn 16 javascript reactjs
我有一个有条件地渲染几个子组件的react组件.简化的代码就是:
render(): {
const component1 = condition ? renderComponent2() : null;
const component2 = renderComponent2();
return (
<div>
{component1}
{component2}
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
问题是component2每次condition更改时都会被破坏并重新呈现.我试图阻止它并保持原始元素.我尝试添加一个key到component2没有运气.
[编辑]这种情况即使component1总是存在也会发生,我在其上更改标记以隐藏或不使用CSS:/
| 归档时间: |
|
| 查看次数: |
8241 次 |
| 最近记录: |