--- title: 'Higher Order Components' --- There are several cases where Struct components dont directly support a feature (ex. Suspense) or require a lot of boiler plate to use the features (ex. Context). In those cases it is recommended to create function components that are higher order components. ## Higher Order Components Definition Higher Order Components are components that dont add any new Html and only wrap some other component to provide extra functionality. ### Example Hook into Context and pass it down to a struct component ```rust use yew::prelude::*; #[derive(Clone, Debug, PartialEq)] struct Theme { foreground: String, background: String, } #[function_component] pub fn App() -> Html { let ctx = use_state(|| Theme { foreground: "#000000".to_owned(), background: "#eeeeee".to_owned(), }); html! { context={(*ctx).clone()}> > } } // highlight-start #[function_component] pub fn ThemedButtonHOC() -> Html { let theme = use_context::().expect("no ctx found"); html! {} } // highlight-end #[derive(Properties, PartialEq)] pub struct Props { pub theme: Theme, } struct ThemedButtonStructComponent; impl Component for ThemedButtonStructComponent { type Message = (); type Properties = Props; fn create(_ctx: &Context) -> Self { Self } fn view(&self, ctx: &Context) -> Html { let theme = &ctx.props().theme; html! { } } } ```