thomasbrus
Hi all,
I’m curious how you would go about the following. I have integrated a React component (Select | Mantine) to select players for a fantasy league team.
This works well but the issue is that each select contains about 600 players and these selects are rendered 11 times on the page.
And since the options are calculated server side and passed to the DOM via data attribute this creates a huge payload and slows down the browser when loading the page.
I tried to turn the React select into a remote select where it fetches the options from the backend but it got really bug, I don’t think this specific 3rd party component is suitable for that use case.
I’m curious if anyone has implemented something similar and what the approach was. Basically I have two options in mind for now:
- Finding another 3rd party React component that actually works with remote data (but integrating it well and styling it correctly gets a bit tiresome)
- Perhaps try out Phoenix LiveView and a less fancy UI, maybe via a modal and a search list such as here: https://blog.devgenius.io/build-a-performat-autocomplete-using-phoenix-liveview-and-alpine-js-8bcbbed17ba7
You’d think that in 2022 it is not so difficult to build a select such as this one but no ![]()
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
derek-zhou
You don’t need React for this. Just build the datalist server side, with the few letters typed by the user as the search criteria to filter down the list to a manageable size. This is one of the cases where Liveview’s live form validation really shines.
thomasbrus
Ah yes, that is clever. I just checked out the phoenix live view demo.
Maybe I’m too picky I think it looks a bit too much like the standard Chrome autofill thing. Can’t seem to style it either.
I also need an ID to be set (in a hidden input) for form submission, rather than just the text. I suppose that’s doable though.
kartheek
Like selects, datalists have fewer styling options available. You cannot style suggestions list.
One alternative to using dynamic selects is allowing user to select players from bigger list is -
popup with input search panel with table below and allowing users to search and select from there.
thomasbrus
Yes that is indeed an option. Although you would somehow need to connect the selected item in the modal back to the form / hidden input.
By the way, I just came across this post which is a bit more what I’m looking for:
My concern though is that there are certain frontend behaviours (such as clicking outside the dropdown to close it) that are not included in this blog post.
And I am not convinced the server should be contacted for such behaviour
thomasbrus
^ I just realized you can solve this client-side via
phx-click-awayand LiveView.JSbenwilson512
Do note that a major upside here is that precisely because it’s standard it will interact better with screen readers, mobile devices, and other accessibility tools.
thomasbrus
For screen readers sure
Although on mobile it does not seem to work so well for the regular user.
benlime
That blog post is a little outdated (I’m the author) and today you could fully rely on
LiveView.JSfor handling the client side part. (I’m gonna revise this post at some time).Do you add the serialised JSON into every instance of the autocomplete field? One approach how you could decrease the payload would be to save it in a simple
scripttag and store the reference inside thedataattribute. Something like this:And then you can create your react component like this.
This way you only send the list once and do not end up with gigantic data attributes. You have to load the data once eventually
(if you don’t filter it on the server)
thomasbrus
I ended up solving it via an async react-select, the react component from Mantine that I was using before just didn’t work well with async data. So that removes the issue of having to load the payload in the DOM initially.
But yes initially it would add the entire payload to every select.
Here is the react-select implementation that I ended up using in case anyone is interested:
react/1is a simple functional component that adds a div withdisplay: contents, adds a Stimulusdata-controllerattribute and serializes props (converting underscore to camelcase, and so on.)The
remote-selectStimulus controller:Notice how it concatenates values via ‘,’ in a single hidden input (in case multiple options are selected).
And finally the react component:
Not much special going on here either except that it uses the
urlproperty to perform the async request