Hooks

Struct Hooks 

Source
pub struct Hooks<'a, 'b: 'a> { /* private fields */ }
Expand description

hooks 管理器,负责组件内所有 hook 的注册、索引和生命周期。

  • 通过 use_hook 注册/获取 hook 实例,保证顺序和类型安全。
  • 支持 context 注入、首次更新标记等。
  • 用户无需手动创建,框架自动管理。

§示例

let mut state = hooks.use_state(|| 0);
let ctx = hooks.use_context::<MyType>();

Implementations§

Source§

impl<'a> Hooks<'a, '_>

Source

pub fn with_context_stack<'c, 'd>( &'c mut self, context: &'c ContextStack<'d>, ) -> Hooks<'c, 'd>

Source

pub fn use_hook<F, H>(&mut self, f: F) -> &mut H
where F: FnOnce() -> H, H: Hook + Unpin + 'static,

Trait Implementations§

Source§

impl<'a> UseContext<'a> for Hooks<'a, '_>

Source§

fn use_context<T: Any>(&self) -> Ref<'a, T>

获取全局/局部上下文,实现依赖注入。适合主题、配置、全局状态等场景。
Source§

fn use_context_mut<T: Any>(&self) -> RefMut<'a, T>

获取可变上下文。
Source§

fn try_use_context<T: Any>(&self) -> Option<Ref<'a, T>>

尝试获取只读上下文,返回 Option。
Source§

fn try_use_context_mut<T: Any>(&self) -> Option<RefMut<'a, T>>

尝试获取可变上下文,返回 Option。
Source§

impl UseEffect for Hooks<'_, '_>

Source§

fn use_effect<F, D>(&mut self, f: F, deps: D)
where F: FnOnce(), D: Hash,

注册同步副作用,依赖变化时自动执行,适合监听状态变化、同步校验等。
Source§

fn use_async_effect<F, D>(&mut self, f: F, deps: D)
where F: Future<Output = ()> + Send + 'static, D: Hash,

注册异步副作用,依赖变化时自动执行,适合异步校验、异步请求等。
Source§

impl UseEvents for Hooks<'_, '_>

Source§

fn use_events<F>(&mut self, f: F)
where F: FnMut(Event) + Send + 'static,

注册全局事件监听器,适合快捷键、全局输入等场景。
Source§

fn use_local_events<F>(&mut self, f: F)
where F: FnMut(Event) + Send + 'static,

注册仅作用于当前组件的事件监听器,适合局部交互。
Source§

impl UseExit for Hooks<'_, '_>

Source§

fn use_exit(&mut self) -> impl FnMut() + Send + 'static

注册退出回调,组件卸载时调用,适合清理资源、保存状态等场景。
Source§

impl UseFuture for Hooks<'_, '_>

Source§

fn use_future<F>(&mut self, f: F)
where F: Future<Output = ()> + Send + 'static,

注册异步副作用任务,适合定时器、网络请求、异步轮询等场景。
Source§

impl UseInsertBefore for Hooks<'_, '_>

Source§

fn use_insert_before(&mut self) -> InsertBeforeHandler

在终端渲染区域前插入内容。
Source§

impl UseMemo for Hooks<'_, '_>

Source§

fn use_memo<F, D, T>(&mut self, f: F, deps: D) -> T
where F: FnOnce() -> T, D: Hash, T: Clone + Send + Unpin + 'static,

依赖缓存,只有依赖变化时才重新计算,适合性能优化。
Source§

impl UseOnDrop for Hooks<'_, '_>

Source§

fn use_on_drop<F>(&mut self, f: F)
where F: FnMut() + Send + 'static,

在组件销毁时执行回调。注意不要在回调中使用State。

Source§

impl UsePreviousSize for Hooks<'_, '_>

Source§

impl UseState for Hooks<'_, '_>

Source§

fn use_state<T, F>(&mut self, init: F) -> State<T>
where F: FnOnce() -> T, T: Unpin + Send + Sync + 'static,

Hooks 提供 use_state 方法,创建响应式状态。 Read more
Source§

impl UseTerminalSize for Hooks<'_, '_>

Source§

fn use_terminal_size(&mut self) -> (u16, u16)

Auto Trait Implementations§

§

impl<'a, 'b> Freeze for Hooks<'a, 'b>

§

impl<'a, 'b> !RefUnwindSafe for Hooks<'a, 'b>

§

impl<'a, 'b> !Send for Hooks<'a, 'b>

§

impl<'a, 'b> !Sync for Hooks<'a, 'b>

§

impl<'a, 'b> Unpin for Hooks<'a, 'b>

§

impl<'a, 'b> !UnwindSafe for Hooks<'a, 'b>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Any for T
where T: Any,

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more