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.

Last Post!

elt547

elt547

In this case, how can I make all the other view helpers easily available in the component modules if I’m doing use Phoenix.Component? It seems like use HydroplaneWeb, :component is the expected usage in phoenix as of right now.

Also should this be the same for use HydroplaneWeb, :live_component and use Phoenix.LiveComponent and the others? Will there be another way to share code between different component modules?

Lastly, is somebody able to explain exactly why the circular dependency was creating that error?

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement