Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use_atom_ref with Fermi gives errors #734

Closed
aimerib opened this issue Jan 9, 2023 · 2 comments · Fixed by #743
Closed

use_atom_ref with Fermi gives errors #734

aimerib opened this issue Jan 9, 2023 · 2 comments · Fixed by #743
Labels
bug Something isn't working fermi Related to the fermi crate

Comments

@aimerib
Copy link

aimerib commented Jan 9, 2023

Problem

When trying to use fermi with a vec atom, the desktop app panics at runtime with error:

thread 'main' panicked at 'No atom root found in context. Did you forget place an AtomRoot component at the top of your app?', /Users/aimeri/.cargo/git/checkouts/dioxus-1e619ce595d3799d/cb7e4f3/packages/fermi/src/hooks/atom_root.rs:10:17

Steps To Reproduce

Here's a simple repro:

Cargo.toml

[package]
name = "fermi-atom-root-repro"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
dioxus = {git = "https://github.com/dioxuslabs/dioxus.git"}
dioxus-desktop = {git = "https://github.com/dioxuslabs/dioxus.git"}
fermi = { git = "https://github.com/dioxuslabs/dioxus.git"}

main.rs

use dioxus::prelude::*;
use dioxus_desktop::{Config, WindowBuilder};
use fermi::*;

fn main() {
    dioxus_desktop::launch_cfg(
        app,
        Config::new()
        .with_window(WindowBuilder::new().with_title("Dioxus"))
        );
}

#[derive(Debug, Clone, Eq, Ord, PartialEq, PartialOrd)]
pub struct Room {
    pub id: u16,
    pub name: String,
    pub description: String,
    pub exits: Vec<String>,
    pub stash: Storage,
    pub room_events: Vec<u16>,
    pub narrative: u16,
    pub subjects: Vec<u16>,
}

#[derive(Debug, Clone, Eq, Ord, PartialEq, PartialOrd, Default)]
pub struct Storage {
    pub items: Vec<String>,
    pub item_ids: Vec<u16>,
}

static ROOMS: AtomRef<Vec<Option<Room>>> = |_| vec![None::<Room>; 81];

pub fn app(cx: Scope) -> Element {
    let rooms = use_atom_ref(cx, ROOMS);

    cx.render(rsx!(
        div {
            rooms.read().iter().map(|f| rsx!{
                if let Some(_) = f {
                    rsx!{
                        div { "hello" }
                    }
                }

            })
        }
    ))
}

NOTE: The fermi.rs example in the main dioxus repository has the exact same behavior, so it also works as a repro for this issue.

Expected behavior

The app should run, and initially I should see nothing on the screen, but if I populated the rooms atom with rooms, I'd see a series of divs with hello on the screen.

Environment:

  • Dioxus version: master
  • Rust version: nightly
  • OS info: MacOS Ventura
  • App platform: desktop
@ealmloff ealmloff added fermi Related to the fermi crate bug Something isn't working labels Jan 9, 2023
@Arhimondus
Copy link

Try to add use_init_atom_root(&cx); at the top of the app function. This is due outdated documentation.

@aimerib
Copy link
Author

aimerib commented Jan 9, 2023

Thank you @Arhimondus!
That worked like a charm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fermi Related to the fermi crate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants