我有一个 MobX 可观察数组,我想用 lodash 的remove删除多个元素。这会导致数组中的每个元素重新渲染。
const array = observable([1,2,3,4,5,1]);
const App = observer(() => {
console.log('Rendering...');
return (
<div>
{ array.map(e => <div> {e} </div>)}
</div>
);
});
ReactDOM.render(
<App />,
document.getElementById('app')
);
如果我尝试删除每次出现的1
,Rendering...
为每个元素记录一次:
_.remove(array, num => num === 1);
> "Rendering..."
> "Rendering..."
> "Rendering..."
> "Rendering..."
> "Rendering..."
> "Rendering..."
我怎样才能使它只重新渲染一次?