the_dude
Liveview - rerendering nonstop (keeps making api request)
Trying to load user data from the reddit api which is working. However, on mount it appears to be stuck in a rendering loop repeatedly making requests to the api. This causes it to error out and redirect to ‘/’. This is what I have below so far… running latest versions of phoenix and elixir 1.10.0. using Tesla for the api requests. i have also tried moving this logic to handle_params instead but does same thing. Any ideas why this is happening? I thought maybe it was making the call twice possibly since it connects to socket twice on initial mount but doesn’t seem to be it. Is it possible this might be due to the way I implemented my api requests?
Other weird thing is that temporary_assigns will not work either…i get an @saves not available in eex template error. I need this to work b/c plan is to append additional items to list w/ “load more” button using phx-update.
Any help appreciated. been digging at this all day and getting nowhere. Also, if there’s a better/cleaner way do this please let me know. a bit green.
def mount(params, _session, socket) do
case Reddit.request_token(params["code"]) do
"invalid_grant" ->
{:ok,
socket
|> put_flash(:error, "Token Invalid!")
|> redirect(to: "/")
}
token ->
{:ok, response} = Reddit.get_user_saves(token)
{:ok, assign(socket, :saves, response.body)}
end
end
First Post!
jhefreyzz
Hi, have you tried using connected? function since liveview is mounted twice.
def mount(params, _session, socket) do
if connected?(socket) do
case Reddit.request_token(params["code"]) do
"invalid_grant" ->
{:ok,
socket
|> put_flash(:error, "Token Invalid!")
|> redirect(to: "/")
}
token ->
{:ok, response} = Reddit.get_user_saves(token)
{:ok, assign(socket, :saves, response.body)}
end
end
end
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








