如何在 LiveView 中更新嵌套赋值的值
How to update the value of nested assigns in LiveView
我正在尝试使用嵌套赋值,但找不到更新其值的方法...
想象一下我有这个:
def mount(socket) do
socket = assign(socket, state: [value1: "20", value2: "50"])
{:ok, socket}
end
如何更新此处的值?
def handle_event("dec", _params, socket) do
socket = assign(socket, state[:value1], "1")
{:noreply, socket}
end
如何reference/represent那个嵌套键?
Kernel.update_in/3
是你的朋友。
state = [value1: "20", value2: "50"]
update_in state, [:value1], & &1 <> "updated"
#⇒ [value1: "20updated", value2: "50"]
旁注:此问题与phoenix-framework无关。
我正在尝试使用嵌套赋值,但找不到更新其值的方法... 想象一下我有这个:
def mount(socket) do
socket = assign(socket, state: [value1: "20", value2: "50"])
{:ok, socket}
end
如何更新此处的值?
def handle_event("dec", _params, socket) do
socket = assign(socket, state[:value1], "1")
{:noreply, socket}
end
如何reference/represent那个嵌套键?
Kernel.update_in/3
是你的朋友。
state = [value1: "20", value2: "50"]
update_in state, [:value1], & &1 <> "updated"
#⇒ [value1: "20updated", value2: "50"]
旁注:此问题与phoenix-framework无关。