javascript - 如何在 FP 中引用当前状态?
问题描述
我已经问过这个问题,但我的解释很糟糕,所以我决定用更好的解释和实际代码再次提问(我会要求版主删除其中一个帖子)。所以让我们考虑一下这个问题。
以下代码段表示来自数组的渲染注释。但是,在添加注释部分期间,我改变了一个状态。所以问题是:如何在不改变的情况下在notes
数组中添加新注释?换句话说,我想删除replaceNotes
并保持相同的功能。我知道完全可以添加没有数组的注释,但是由于将来的参考,我确实需要使用注释更新数组。问题是,在我的原始应用程序中,我有带有注释的列表,当我在列表之间切换时,我应该得到依赖于我打开的列表的渲染注释。这就是为什么我应该保留对notes
数组的引用。
同时我想知道,如果我只是存储然后从这些数据notes
中做笔记,可以吗?localStorage
这是函数式编程的好习惯吗?
const button = document.getElementById('button');
const notesContainer = document.querySelector('.notes');
const pipe = (f, g) => (...args) => f(g(...args));
let notes = [];
const createNote = (...fns) => fns.reduceRight(pipe);
const handleEvent = () =>
createNote(gatherContent, renderContent, replaceNotes)(notes);
function gatherContent(notes) {
const name = prompt('How do you want to name a note?');
return [...notes, { name }];
}
function renderContent(notes) {
function render(note) {
const noteEl = document.createElement('div');
noteEl.innerHTML = `<p>${note.name}</p>`;
notesContainer.append(noteEl);
}
notesContainer.innerHTML = '';
notes.map(render);
return notes;
}
const replaceNotes = newNotes => (notes = newNotes);
button.addEventListener('click', handleEvent);
<button id="button">Click me!</button>
<section class="notes"></section>
解决方案
以下是如何创建一个简单的任务列表应用程序,而不需要改变除 DOM 之外的任何内容。
const button = document.getElementById("button");
const section = document.getElementById("notes");
const template = document.getElementById("template");
template.parentNode.removeChild(template);
const render = notes => {
button.onclick = event => {
const name = prompt("How do you want to name a note?");
render([...notes, { name }]);
};
while (section.lastChild) {
section.removeChild(section.lastChild);
}
for (const note of notes) {
const node = template.cloneNode(true);
node.firstChild.firstChild.nodeValue = note.name;
section.appendChild(node);
}
};
render([]);
<button id="button">Click me!</button>
<section id="notes"></section>
<div id="template"><p>name</p></div>
有关详细说明,请阅读我之前的答案。https://stackoverflow.com/a/58642199/783743
您也可以使用此模式localStorage
。
const button = document.getElementById("button");
const section = document.getElementById("notes");
const template = document.getElementById("template");
template.parentNode.removeChild(template);
const render = notes => {
localStorage.setItem("notes", notes); // set notes
button.onclick = event => {
const name = prompt("How do you want to name a note?");
render([...notes, { name }]);
};
while (section.lastChild) {
section.removeChild(section.lastChild);
}
for (const note of notes) {
const node = template.cloneNode(true);
node.firstChild.firstChild.nodeValue = note.name;
section.appendChild(node);
}
};
render(localStorage.getItem("notes") || []); // get notes
请注意,它localStorage
只能用于保存要跨会话使用的状态。不建议localStorage
用作您的应用商店。这将导致糟糕的性能和糟糕的代码结构。
推荐阅读
- java - Spring Boot Admin 应用程序 - 使用 InstanceExchangeFilterFunction 将蛇形大小写转换为 UI 的驼峰形大小写
- python - 基于另一列值的数据框中的唯一行
- dialogflow-es - DialogFlow:后续意图自动移出到根级别
- .net - 通过 ajax 调用 dotnet 核心操作与 Visual Studio 一起使用,但在 iis 上托管后无法正常工作获取状态代码 404
- javascript - MongoServerError:对路径“_id”执行更新将修改不可变字段“_id”
- r - 如何查找值是否与上一行不同并在下一行重复该信息(如循环)?
- node.js - 使用独立 API 保护单页应用程序免受 XSRF 影响
- python - 如何在 Python (BeaufitulSoup) 中在 CSV 中为多网址刮板附加新行?
- coffeescript - tex 文件的原子片段
- cartopy - Mac OS 上的 Cartopy 安装:Python 3.8 和 Matplotlib 3.3.3