1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate;
/// Convert a borrowed value (`&T`) into a component-scoped [`State<T>`].
///
/// Useful when a component owns fields (props) and you need a reactive `State<T>` that can
/// be passed to hooks, effects, or child components without cloning repeatedly.
///
/// The returned `State<T>` is initialized by cloning the provided `value`. On subsequent
/// renders the internal state will be updated to follow `value` whenever it changes
/// (using `PartialEq` to avoid unnecessary updates).
///
/// Example
/// ```rust,no_run
/// # use freya::prelude::*;
/// #[derive(Clone, PartialEq)]
/// struct Config {
/// value: i32,
/// }
///
/// #[derive(PartialEq)]
/// struct MyComponent {
/// config: Config,
/// }
///
/// impl Component for MyComponent {
/// fn render(&self) -> impl IntoElement {
/// let config = use_reactive(&self.config);
///
/// use_side_effect(move || {
/// // `.read()` subscribes the effect to changes of `config`
/// let config = config.read();
/// println!("config value: {}", config.value);
/// });
///
/// rect()
/// }
/// }
/// ```
///
/// Notes:
/// - Call `use_reactive` at the top level of your component's `render` method like other hooks.
/// - The hook avoids extra cloning by only setting the internal state when `value` differs.