#http-request #hook #dioxus #react #ui

dioxus-use-request

一组用于执行简单HTTP请求的Dioxus钩子

3个不稳定版本

0.2.0-alpha052022年9月17日
0.2.0-alpha04 2022年8月24日
0.1.5 2022年8月21日

#724 in WebAssembly

每月35次下载

MIT/Apache

9KB
56

Dioxus - 使用请求钩子

Build Status Publish Status Language: Rust Crates.io Version

一个小型crate,为Dioxus添加了用于简单GET请求的钩子,以及便捷宏

示例

函数

像这样使用钩子...

let dog = use_request(&cx, (breed,), format!("https://dog.ceo/api/breed/{breed}/images/random"));

cx.render(match dog {
    Success(val) => rsx!(img { src: "{val.message}"}}),
    Failure(err) => rsx!("Failed to load dog"),
    Loading => rsx!("Loading dog image!"),
})

...与使用Use Future Hook的片段达到相同效果

let dog = use_future(&cx, (breed,), |(breed)| async move {
        reqwest::get(format!("https://dog.ceo/api/breed/{breed}/images/random"))
            .await
            .unwrap()
            .json::<RandomDog>()
            .await
    });
    
cx.render(match dog.value() {
    Some(Ok(val)) => rsx!(img { src: "{val.message}"}}),
    Some(Err(err)) => rsx!("Failed to load dog"),
    None => rsx!("Loading dog image!"),
})

为了额外的便捷性,您还可以启用use_request!宏。

[dependencies]
dioxus-use-request = { version = "0.1.4", features = ["macro"] }

它会自动从给定的URL字符串中提取钩子的依赖项。使用宏,上面的示例看起来像这样

// Macro automatically extracts dependencies in curly braces (e.g. {breed}) from the given string literal
let dog = use_request!(cx, "https://dog.ceo/api/breed/{breed}/images/random");

cx.render(match dog {
    Success(val) => rsx!(img { src: "{val.message}"}}),
    Failure(err) => rsx!("Failed to load dog"),
    Loading => rsx!("Loading dog image!"),
})

安装

有关开始使用Dioxus的说明,请参阅他们的入门指南。要使用此crate,只需将其添加到Cargo.toml中的依赖项中。如果您想使用use_request!宏,请确保启用“macro”功能

[dependencies]
dioxus-use-request = { version = "0.1.4", features = ["macro"] }

功能

  • 实现钩子
  • 具有依赖项提取的便捷宏
  • 返回UseRequest句柄,使请求可重启动
  • UseRequest句柄上实现.split(),它返回值和重启动闭包

如果您遇到错误或对功能有想法,请随时打开问题

许可证

此代码为双许可,可根据您的需求选择MIT许可证或Apache 2.0许可证。

Apache2.0许可证

Copyright 2022 Antonius Naumann

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   https://apache.ac.cn/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

MIT许可证

MIT License

Copyright (c) 2022 Antonius Naumann

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

依赖项

~7–20MB
~311K SLoC