elt547

elt547

Trying to make my function components available everywhere, getting an error `module_info/1` is undefined?

I’m trying to get my function components available in all of my views. My shared components are in there own files under /live/components/_shared. And I have one shared.ex file available under /live/components/shared.ex. I don’t know enough about macros in elixir yet.

This is what it looks like:

defmodule HydroplaneWeb.Components.Shared do
  alias HydroplaneWeb.Components.Shared

  defmacro __using__(_opts) do
    quote do
      import Shared.{Button, Card, Dropdown, Lists, Modal}
    end
  end
end

But when I use it from my view_helpers() it is giving me the error Shared.Button.module_info/1 is undefined (function not available).

  defp view_helpers do
    quote do
      # ..................

      use HydroplaneWeb.Components.Shared
    end
  end

I have tried a few different configurations but just know I’m missing something basic. How can I get this working so that all of my function components are available in my liveview templates? Also what key concept am I misunderstand so I can avoid the same mistake in the future?

Marked As Solved

Nicd

Nicd

Is def components in HydroplaneWeb using view_helpers() also? Then you’re running into a circle when compiling: view_helpers → import HydroplaneWeb.Components.Shared.Button → use :components → view_helpers.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

The alias HydroplaneWeb.Components.Shared you have at the top of the file is not in the same scope as your import Shared line. You can see this in the error message in that it is Shared.Button.module_info/1 not HydroplaneWeb.Components.Shared.module_info/1.

You should either make the import fully qualified eg import HydroplaneWeb.Components.Shared.{Button, Card, Dropdown, Lists, Modal} or add the alias inside the quote. I’d recommend just making it fully qualified so you don’t inject an alias into the other module.

krstfk

krstfk

Can you share your button module?

krstfk

krstfk

That’s what I think as well.

josevalim

josevalim

Creator of Elixir

Right!

Suggestion: don’t use use HydroplaneWeb, :component. We will most likely do use Phoenix.Component itself in new Phoenix apps on each component module and favor explicit imports per module.

Also 1 module with multiple shared components is better than 10 modules with individual components. :slight_smile:

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement