it('should render a simple component', () => {
function Child() {
return <div>Hello World</div>;
}
function Parent() {
return <Child />;
}
ReactNoopPersistent.render(<Parent />);
expect(ReactNoopPersistent.flushWithHostCounters()).toEqual({
hostDiffCounter: 0,
hostCloneCounter: 0,
});
ReactNoopPersistent.render(<Parent />); //
expect(ReactNoopPersistent.flushWithHostCounters()).toEqual({
hostDiffCounter: 1,
hostCloneCounter: 1,
});
// Child() === Child() 为 false
});
使用代码测试 Child() === Child() 为 false。
it('should not diff referentially equal host elements', () => {
function Leaf(props) {
return (
<span>
hello
<b />
{props.name}
</span>
);
}
const constEl = (
<div>
<Leaf name="world" />
</div>
);
function Child() {
return constEl;
}
function Parent() {
return <Child />;
}
ReactNoopPersistent.render(<Parent />);
expect(ReactNoopPersistent.flushWithHostCounters()).toEqual({
hostDiffCounter: 0,
hostCloneCounter: 0,
});
ReactNoopPersistent.render(<Parent />);
expect(ReactNoopPersistent.flushWithHostCounters()).toEqual({
hostDiffCounter: 0,
hostCloneCounter: 0,
});
});
// Child() === Child() 为 true
使用代码测试 Child() === Child() 为 true。没有进行 diff 与 clone。
clone 的 ReactElemnt
it('should not diff parents of setState targets', () => {
let childInst;
function Leaf(props) {
return (
<span>
hello
<b />
{props.name}
</span>
);
}
class Child extends React.Component {
state = {name: 'Batman'};
render() {
childInst = this;
return (
<div>
<Leaf name={this.state.name} />
</div>
);
}
}
function Parent() {
return (
<section>
<div>
<Leaf name="world" />
<Child />
<hr />
<Leaf name="world" />
</div>
</section>
);
}
ReactNoopPersistent.render(<Parent />);
expect(ReactNoopPersistent.flushWithHostCounters()).toEqual({
hostDiffCounter: 0,
hostCloneCounter: 0,
});
childInst.setState({name: 'Robin'});
expect(ReactNoopPersistent.flushWithHostCounters()).toEqual({
// section > div > Child > div
// section > div > Child > Leaf > span
// section > div > Child > Leaf > span > b
hostDiffCounter: 3,
// section
// section > div
// section > div > Child > div
// section > div > Child > Leaf > span
// section > div > Child > Leaf > span > b
hostCloneCounter: 5,
});
ReactNoopPersistent.render(<Parent />);
expect(ReactNoopPersistent.flushWithHostCounters()).toEqual({
// Parent > section
// Parent > section > div
// Parent > section > div > Leaf > span
// Parent > section > div > Leaf > span > b
// Parent > section > div > Child > div
// Parent > section > div > Child > div > Leaf > span
// Parent > section > div > Child > div > Leaf > span > b
// Parent > section > div > hr
// Parent > section > div > Leaf > span
// Parent > section > div > Leaf > span > b
hostDiffCounter: 10,
hostCloneCounter: 10,
});
});
这个 高效Update 中有说明。此处的clone更指向于 beginWork —> bailoutOnAlreadyFinishedWork