Skip to content

Instantly share code, notes, and snippets.

@nyanpasu64
Created January 10, 2020 19:11
Show Gist options
  • Save nyanpasu64/d7b61d60cc65ecb7d862597d40e9d24b to your computer and use it in GitHub Desktop.
Save nyanpasu64/d7b61d60cc65ecb7d862597d40e9d24b to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Test in Browser</title>
<script src="https://cdn.jsdelivr.net/npm/object-assign-polyfill@0.1.0/index.min.js"></script>
<script src="./dist/immer.umd.js"></script>
</head>
<body>
<script>
try {
let orig = [[0]];
orig.push(orig[0]);
// 0,0
// Why not console.log(orig) directly? Mutating orig at the end of this code alters logs from earlier on!
console.log(orig + "");
function try_it(fn) {
const result = immer.produce(orig, fn)
console.log(result + "")
}
// 1,0
try_it(function(draft) {
draft[0][0] = 1;
});
// 1,1
try_it(function(draft) {
draft[1] = draft[0];
draft[0][0] = 1;
});
// 1,1
orig[0][0] = 1;
console.log(orig + "");
} catch (err) {
alert(err)
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment