{"record":{"id":"90572b5edc4a915e","repo":"DioxusLabs/dioxus","slug":"must-be-called-from-inside-a-dioxus-runtime-help","errorCode":null,"errorMessage":"Must be called from inside a Dioxus runtime.\n\nHelp: Some APIs in dioxus require a global runtime to be present.\nIf you are calling one of these APIs from outside of a dioxus runtime\n(typically in a web-sys closure or dynamic library), you will need to\ngrab the runtime from a scope that has it and then move it into your\nnew scope with a runtime guard.\n\nFor example, if you are trying to use dioxus apis from a web-sys\nclosure, you can grab the runtime from the scope it is created in:\n\n```rust\nuse dioxus::prelude::*;\nstatic COUNT: GlobalSignal<i32> = Signal::global(|| 0);\n\n#[component]\nfn MyComponent() -> Element {\n    use_effect(|| {\n        // Grab the runtime from the MyComponent scope\n        let runtime = Runtime::current().expect(\"Components run in the Dioxus runtime\");\n        // Move the runtime into the web-sys closure scope\n        let web_sys_closure = Closure::new(|| {\n            // Then create a guard to provide the runtime to the closure\n            let _guard = RuntimeGuard::new(runtime);\n            // and run whatever code needs the runtime\n            tracing::info!(\"The count is: {COUNT}\");\n        });\n    })\n}\n```","messagePattern":"Must be called from inside a Dioxus runtime\\.\n\nHelp: Some APIs in dioxus require a global runtime to be present\\.\nIf you are calling one of these APIs from outside of a dioxus runtime\n\\(typically in a web-sys closure or dynamic library\\), you will need to\ngrab the runtime from a scope that has it and then move it into your\nnew scope with a runtime guard\\.\n\nFor example, if you are trying to use dioxus apis from a web-sys\nclosure, you can grab the runtime from the scope it is created in:\n\n```rust\nuse dioxus::prelude::\\*;\nstatic COUNT: GlobalSignal<i32> = Signal::global\\(\\|\\| 0\\);\n\n#\\[component\\]\nfn MyComponent\\(\\) -> Element \\{\n    use_effect\\(\\|\\| \\{\n        // Grab the runtime from the MyComponent scope\n        let runtime = Runtime::current\\(\\)\\.expect\\(\"Components run in the Dioxus runtime\"\\);\n        // Move the runtime into the web-sys closure scope\n        let web_sys_closure = Closure::new\\(\\|\\| \\{\n            // Then create a guard to provide the runtime to the closure\n            let _guard = RuntimeGuard::new\\(runtime\\);\n            // and run whatever code needs the runtime\n            tracing::info!\\(\"The count is: \\{COUNT\\}\"\\);\n        \\}\\);\n    \\}\\)\n\\}\n```","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/runtime.rs","lineNumber":118,"sourceCode":"            scope_states: Default::default(),\n            scope_stack: Default::default(),\n            suspense_stack: Default::default(),\n            current_task: Default::default(),\n            tasks: Default::default(),\n            suspended_tasks: Default::default(),\n            pending_effects: Default::default(),\n            dirty_tasks: Default::default(),\n            render_targets: RefCell::new(render_targets),\n            mounts: Default::default(),\n        })\n    }\n\n    /// Get the current runtime\n    pub fn current() -> Rc<Self> {\n        RUNTIMES\n            .with(|stack| stack.borrow().last().cloned())\n            .unwrap_or_else(|| {\n                panic!(\n                    \"Must be called from inside a Dioxus runtime.\n\nHelp: Some APIs in dioxus require a global runtime to be present.\nIf you are calling one of these APIs from outside of a dioxus runtime\n(typically in a web-sys closure or dynamic library), you will need to\ngrab the runtime from a scope that has it and then move it into your\nnew scope with a runtime guard.\n\nFor example, if you are trying to use dioxus apis from a web-sys\nclosure, you can grab the runtime from the scope it is created in:\n\n```rust\nuse dioxus::prelude::*;\nstatic COUNT: GlobalSignal<i32> = Signal::global(|| 0);\n\n#[component]\nfn MyComponent() -> Element {{\n    use_effect(|| {{","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/DioxusLabs/dioxus/blob/24f6a829df0dfa203961a98ea4cae21c2ff27e28/packages/core/src/runtime.rs#L100-L136","documentation":"Error \"Must be called from inside a Dioxus runtime.\n\nHelp: Some APIs in dioxus require a global runtime to be present.\nIf you are calling one of these APIs from outside of a dioxus runtime\n(typically in a web-sys closure or dynamic library), you will need to\ngrab the runtime from a scope that has it and then move it into your\nnew scope with a runtime guard.\n\nFor example, if you are trying to use dioxus apis from a web-sys\nclosure, you can grab the runtime from the scope it is created in:\n\n```rust\nuse dioxus::prelude::*;\nstatic COUNT: GlobalSignal<i32> = Signal::global(|| 0);\n\n#[component]\nfn MyComponent() -> Element {\n    use_effect(|| {\n        // Grab the runtime from the MyComponent scope\n        let runtime = Runtime::current().expect(\"Components run in the Dioxus runtime\");\n        // Move the runtime into the web-sys closure scope\n        let web_sys_closure = Closure::new(|| {\n            // Then create a guard to provide the runtime to the closure\n            let _guard = RuntimeGuard::new(runtime);\n            // and run whatever code needs the runtime\n            tracing::info!(\"The count is: {COUNT}\");\n        });\n    })\n}\n```\" thrown in DioxusLabs/dioxus.","triggerScenarios":"Thrown at packages/core/src/runtime.rs:118 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Call the API from inside a Dioxus runtime. From foreign scopes (web-sys closures, dynamic libraries), capture Runtime::current() where it exists and enter it with RuntimeGuard::new(runtime)."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"24f6a829df0dfa203961a98ea4cae21c2ff27e28","analyzedAt":"2026-08-23T07:10:14.078Z","contentChangedAt":"2026-08-23T07:10:14.078Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}