我在 SolidJS 中有一个组件,看起来像这样:
const MyComponent: Component = (params) => {
// ...
const [votes, setVotes] = createSignal(new Set());
const toggle = (val: string) => {
if (votes().has(val)) {
let _votes = votes();
_votes.delete(val);
setVotes(_votes);
} else {
let _votes = votes();
_votes.add(val);
setVotes(_votes);
}
}
return <>
<For each={someArray()}>{(opt, i) =>
<button
style={{
color: `${ votes().has(opt) ? "blue" : "black"}`
}}
onClick={() => { toggle(opt) } }
>
{opt()}
</button>
}</For>
</>
}
Run Code Online (Sandbox Code Playgroud)
我想要发生的是,按钮的样式将根据它(opt在 for 循环中)是否存在于集合中而改变votes。 …