<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="178040" data-post-id="178040">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="PJextra" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  PJextra
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="LostKobrakai" data-post="5" data-topic="31920">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/48/3072_2.png" class="avatar"> LostKobrakai:</div>
<blockquote>
<p>If those points are not relevant you can just load stuff for the websockets connection and make the static render return just a loading screen.</p>
</blockquote>
</aside>
<p>How can you define different things to render in the first and on the second rendering? I though we could only use one template (assuming you have a live route) and as such you would need to render the same content.<br>
Could you point some example/info, please?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="178040" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/mount-vs-handle-params-on-the-liveview-life-cycle/31920/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-178040" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="178040"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="178042" data-post-id="178042">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="PJextra" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  PJextra
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Missed this link in the thread…it’s explained here: <a href="https://forum.elixirforum.com/t/shortcomings-in-liveview/31831/10" class="inline-onebox" rel="nofollow">Shortcomings in LiveView - are there any I should look out for? - #10 by josevalim</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="178042" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/mount-vs-handle-params-on-the-liveview-life-cycle/31920/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-178042" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="178042"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="184658" data-post-id="184658">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sodapopcan" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/120/34668_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sodapopcan
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I have a page that loads content based on the first URL parameter and uses <code>live_patch</code> in its navigational event handlers—If there is no parameter, then it provides a default.</p>
<p>Knowing that <code>handle_params</code> always gets called and I don’t care about checking any <code>mount</code>-type behaviour (like <code>socket_connected?</code>) should  this golden rule still apply?  I found I was able to simplify my code (and still have it work with JS disabled) with the following:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @default_assigns [
    entity: DB.find("default_entity"),
    # ... more keyword list entries
  ]

  @impl true
  def handle_params(%{"term" =&gt; term}, _session, socket), do:
    {:noreply, assign(socket, @default_assigns |&gt; Keyword.replace!(:entity, DB.find(term)))}
  def handle_params(_params, _session, socket), do:
    {:noreply, assign(socket, @assigns)}

  @impl true
  def handle_event("submit", %{"term" =&gt; entity}, socket), do:
    {:noreply, push_patch(socket, to: "/#{entity}", replace: true)}
</code></pre>
<p>The site in question is an art project and this works so there is nothing at stake here, I’m just trying to grok best practices.</p>
<p>Note: <code>DB</code> is just an example and not something that makes a database call (even Ecto isn’t involved).  I’m pretty sure I can understand why setting attribute values based on a db lookup is probably a bad idea… but is it??</p>
<p>Ok, thanks!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="184658" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/mount-vs-handle-params-on-the-liveview-life-cycle/31920/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-184658" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="184658"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="184664" data-post-id="184664">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I would say your snippet is correct. Since that particular parameter/assign can change, it should be loaded in mount instead of handle_params.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="184664" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/mount-vs-handle-params-on-the-liveview-life-cycle/31920/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-184664" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="184664"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="184708" data-post-id="184708">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sodapopcan" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/120/34668_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sodapopcan
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thank you for the response!</p>
<p>Did you mean my snippet is incorrect?</p>
<p>My thing is that I want to handle loading <code>/</code> and <code>/:entity</code>, so to handle it in mount, I’d need my two implementations of <code>mount</code> to handle them (<code>def mount(%{"entity" =&gt; entity}, ...)</code> and <code>def mount(_params, ...)</code>) and then that exact same handling logic would be repeated in equivalent <code>handle_params</code> implementations.  Since <code>handle_params</code> gets calls after <code>mount</code> anyway, why should I bother assigning assigns in <code>mount</code> in this case?</p>
<p>Thanks again!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="184708" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/mount-vs-handle-params-on-the-liveview-life-cycle/31920/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-184708" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="184708"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="184743" data-post-id="184743">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Your snippet is correct. <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"> the params that can change are loaded on handle_params.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="184743" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/mount-vs-handle-params-on-the-liveview-life-cycle/31920/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-184743" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="184743"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="184754" data-post-id="184754">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sodapopcan" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/120/34668_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sodapopcan
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thank you so much for your response.  I realize I’m being somewhat pedantic though I’m quite new to functional programming (under a year) and want to ensure I understand the paradigms and avoid shoehorning my OO and Rails knowledge into Elixir and Phoenix.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="184754" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/mount-vs-handle-params-on-the-liveview-life-cycle/31920/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-184754" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="184754"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #17"></div>
  </section>
</div>
    <div class="postbit" id="239401" data-post-id="239401">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="tfwright" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tfwright/120/20299_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  tfwright
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hoping someone can check my work on this issue, since I haven’t had the opportunity to work with LV in production much:</p>
<p>I ran into an issue where I had some data that needed to be loaded in different ways depending on the content of the params, and if it was not loaded correctly, it would fail. I had moved all the logic into <code>handle_params</code> already, but because on initial load the query params were not passed, and since the <em>absence</em> of the query param is a valid option in some cases, I couldn’t rely on pattern matching to always skip the logic in question as I have in other cases in the past. As a result certain views would break because it would try to execute the code as if the param were absent even when it was actually present in the uri.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def handle_params(params = %{"path_param" =&gt; needs_y, "x" =&gt; y}, _, socket) do
  socket = assign(socket, :data, load_func(needs_y, y))
  {:noreply, socket}
end

def handle_params(params = %{"path_param" =&gt; no_y_needed}, _, socket) do
  socket = assign(socket, :data, load_func(no_y_needed) # &lt;- this fails on initial render before "x" is passed
  {:noreply, socket}
end
</code></pre>
<p>One option I considered was actually parsing the URI directly to determine whether param was present or not and wait until it was passed if so:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def handle_params(params = %{"path_param" =&gt; maybe_needs_y}, uri, socket) do
  if String.contains?(uri, "x") do
    if x = params["x"], do: assign(socket, :data, load_func(maybe_needs_y, x), else: {:noreply, socket} #wait
  else
    assign(socket, :data, load_func(maybe_needs_y)
  end
end
</code></pre>
<p>…but that seemed awkward and potentially fragile.</p>
<p>So instead what I did was introduce a loading param that I set to the value of <code>connected/?</code> in the <code>mount</code> function.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def mount(_, _, socket) do
  # other stuff
  socket = assign(socket, :loading, !connected?(socket))
  {:ok, socket}
end

def handle_params(_, _, %{loading: true}), do: {:noreply, socket} # wait

def handle_params(params, _, socket) do
  # query params present, exec loading data
end
</code></pre>
<p>So far this seems to be working but my sense is that this may not be what the <code>connected?</code> check is intended to be used for.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="239401" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/mount-vs-handle-params-on-the-liveview-life-cycle/31920/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-239401" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="239401"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>