[英]How to update the value of nested assigns in LiveView
I'm trying to use nested assigns but cannot find a way to update its values… Imagine I have this:我正在尝试使用嵌套分配,但找不到更新其值的方法......想象一下我有这个:
def mount(socket) do
socket = assign(socket, state: [value1: "20", value2: "50"])
{:ok, socket}
end
How do I update the value here?如何在此处更新值?
def handle_event("dec", _params, socket) do
socket = assign(socket, state[:value1], "1")
{:noreply, socket}
end
How do I reference/represent that nested key?如何引用/表示该嵌套键?
Kernel.update_in/3
is your friend here. Kernel.update_in/3
是你的朋友。
state = [value1: "20", value2: "50"]
update_in state, [:value1], & &1 <> "updated"
#⇒ [value1: "20updated", value2: "50"]
Sidenote: this question has nothing to do with phoenix-framework .旁注:这个问题与phoenix-framework无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.