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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//! # Context
//!
//! Dioxus offers a way to pass data from parent components to its descendents in a way to avoid [**Prop Drilling**](#prop-drilling).
//!
//! ## Prop Drilling
//!
//! **Prop drilling** is when you want to pass a certain data from one parent component to some nested component, and you start to declare the same prop in each one of the components in between the parent and the target component. This causes a huge unnecessary boilerplate that can be used by using the Context API.
//!
//! ```rust
//! # use freya::prelude::*;
//! // Parent component
//! #[component]
//! fn CompA() -> Element {
//!     rsx!(
//!         CompB {
//!             value: 2
//!         }
//!     )
//! }
//!
//! // This component needs the value just so it can pass it to the next component
//! #[component]
//! fn CompB(value: usize) -> Element {
//!     rsx!(
//!         CompC {
//!             value
//!         }
//!     )
//! }
//!
//! // Same as CompB
//! #[component]
//! fn CompC(value: usize) -> Element {
//!     rsx!(
//!         CompD {
//!             value
//!         }
//!     )
//! }
//!
//! // Finally ! Our target component
//! #[component]
//! fn CompD(value: usize) -> Element {
//!     rsx!(
//!         label {
//!             "{value}"
//!         }
//!     )
//! }
//! ```
//!
//! ## Context
//!
//! You can initialize a context that will be identified by its type and be allowed to be accessed from any desdendent from where you intialized it.
//!
//! ```rust
//! # use freya::prelude::*;
//! // Parent component
//! #[component]
//! fn CompA() -> Element {
//!     // Initialize the context with `1` usize as value
//!     // Any component desdendant of `CompA` will be allowed to access this component
//!     use_context_provider(|| 1);
//!
//!     rsx!(
//!         CompB { }
//!     )
//! }
//!
//! #[component]
//! fn CompB() -> Element {
//!     rsx!(
//!         CompC { }
//!     )
//! }
//!
//! #[component]
//! fn CompC() -> Element {
//!     rsx!(
//!         CompD { }
//!     )
//! }
//!
//! // Our target component
//! #[component]
//! fn CompD() -> Element {
//!     // Retrieve our context as we know its type
//!     let value = use_context::<usize>();
//!
//!     rsx!(
//!         label {
//!             "{value}"
//!         }
//!     )
//! }
//! ```
//!
//! ### Avoid having context with same type
//!
//! Because Context are identified by their type, you cannot do the following:
//!
//! ```rust
//! # use freya::prelude::*;
//! // Parent component
//! #[component]
//! fn CompA() -> Element {
//!     use_context_provider(|| 1);
//!     use_context_provider(|| 2);
//!     use_context_provider(|| 3);
//!
//!     // All these 3 contexts share the same type, `usize`, so each context will replace any other context defined previously, which means that only the third context will actually be accessible
//!
//!     rsx!(
//!         CompB { }
//!     )
//! }
//!
//! # #[component]
//! # fn CompB() -> Element {
//! #    None
//! # }
//! ```
//!
//! If you really need to the tree contexts split you can wrap them in different types so each one gets an unique type instead of just `usize`.
//!
//! ```rust
//! # use freya::prelude::*;
//!
//! #[derive(Clone)]
//! struct ValueA(pub usize);
//!
//! #[derive(Clone)]
//! struct ValueB(pub usize);
//!
//! #[derive(Clone)]
//! struct ValueC(pub usize);
//!
//! // Parent component
//! #[component]
//! fn CompA() -> Element {
//!     use_context_provider(|| ValueA(1));
//!     use_context_provider(|| ValueB(2));
//!     use_context_provider(|| ValueC(3));
//!
//!     // All these 3 contexts share the same type, `usize`, so each context will replace any other context defined previously, which means that only the third context will actually be accessible
//!
//!     rsx!(
//!         CompB { }
//!     )
//! }
//!
//! #[component]
//! fn CompB() -> Element {
//!     let value_a = use_context::<ValueA>();
//!     let value_b = use_context::<ValueB>();
//!     let value_c = use_context::<ValueC>();
//!     rsx!(
//!         label {
//!             "{value_a.0} {value_b.0} {value_c.0}"
//!         }
//!     )
//! }
//! ```