patch()
Syntax: patch(oldBlock, newBlock)
Example: patch(block1, block2)
The patch
function is used to rerender a block with another block. The oldBlock
is the block that will be rerendered, and the newBlock
is the block that will replace it.
💡
Always try to keep Blocks of the same shape when using patch in order to maintain good performance.
import { block, mount, patch, fragment } from 'million';
const display = block(({ text }) => {
return <p>{text}</p>;
});
const bigDisplay = block(({ text }) => {
return <h1>{text}</h1>;
});
const main = display({ text: 'Hello' });
mount(main, document.getElementById('root')); // optional
patch(main, display({ text: 'World' }));
patch(main, bigDisplay({ text: 'World' })); // inefficent, but works