shahryarjb

shahryarjb

I created an issue in LiveView GitHub: When `live_flash` is clicked, it cleans `temporary_assigns` inside a `live_component` · Issue #1960 · phoenixframework/phoenix_live_view · GitHub

My problem comes when I click on a live_flash.

I described in this video

And Jose told me:

This is intentional. if you declare something as temporary it will be cleared immediately after rendering and any other event won’t have that date anymore. You need to make sure that the variable is being properly changed tracked so it is not rerendered unless the variable itself changes.

But I could not understand how to track my variable. All the things work but when a user click on live_flash it clears all the temporary_assigns.

Thank you

Showing Posts 1 to 10

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Can you use a regular assign then not a temporary assign?

shahryarjb

shahryarjb OP

Hi, I think for now no!! Because in my real-project it needs many changes. But as I said when I am using regular assign I have no problems.
Temporary solution that I test in a demo application is using phx-hook to send a request to js and delete the element like this:

let Hooks = {}
Hooks.DeleteFlashMessage = {
    mounted() {
      this.handleEvent("flash_message", ({id}) => {
          console.log(id)
        const element = document.getElementById(id);
        element.remove();
      });
    }
}

As Jose said

I would try to rely more on regular components.

I did not do this in my project, I have many nested LiveView component to create modular UI and let developer create a duplicate page very fast, and it is impossible to change all of them for now.

You are passing the @flash and @notifs to the component, which means that, if any of those change, the other will be re-rendered.
You are also using many stateful components, which means each of them is having their own version of @notifs

Unfortunately, before update t worked for me, but I think some basic context were changed like live_flash can re-render my live_component

For example:

I am using temporary assign in my project because always the selected parts are updated, and I do not want to re-render or re-call all the database to improve my using resource. But some places in my project just was for theory, and they forced me to load all the data again even I used temporary assign there.

After 16 months of working on LiveView every day, I think the context changes are very high and can destroy release time, but another hand it is going to continue very excellent, and I am very grateful of LiveView team.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

If you use a stateful component you don’t need to hit the database again either. Using temporary assigns this way is not really how they’re intended to be used, you need to be combining them with phx-update=append or prepend. They’re not a way to optimize out DB calls from stateless components.

shahryarjb

shahryarjb OP

Yes I have done like you said, but some places like changing like number of a post in a list are going to be a problem or bookmark a post and change its icon to bookmarked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Can you elaborate on what those problems are?

shahryarjb

shahryarjb OP

For example, you have 50 posts with pagination, every page shows 15 post and somebody in the page 2 like a post and another people like a post in page 1. So I want every client in ever page can see a current like number of a post.
Like Twitter, you can see like number of a tweet real-time. But there is a difference, Twitter push all the tweets in a page, but I have pagination.

Please see this image (it is Persian but the heart is like)

How can update it without re-call from db. I create a Pubsub and push an event and get this in my LiveView handle_info if a post likes changed and my user in a page the like is changed I re-bind.

Code:

tfwright

tfwright

If you are just trying update a small piece of markup like the “bookmark” icon or something similar, have you considered just handling that manually in js, and using phx-ignore to protect it from LV updates?

shahryarjb

shahryarjb OP

Yes, I did this !! After the problem I sent in first post, I handle my problem with phoenix LiveView hook.

And for Like and Bookmark, I will do with phx-hook in new version of my CMS, but using phx-ignore no I did not, I will try!! For now, phx-hook fixed my issue.

I wanted to do this with LiveView because I have no information with JS security problems

tfwright

tfwright

Sorry, I was having a hard time understanding the exact nature of your use case. FWIW, I have rarely needed to use temporary_assigns because reloading a paginated set of db records is not usually a very expensive operation. And if it is, you can manually manage the list in other ways. If I am not misunderstanding, it sounds like, as others have pointed out, you have been using temporary_assigns for something it was not intended for and may have been only accidentally working. So if you want to avoid hooks you possibly can just switch to assigns?

shahryarjb

shahryarjb OP

For now, I do not think so, But can you give me a real world example where you use temporary_assigns?

Thank you for your consideration

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews