You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Text component uses the context TextAncestorContext to determine if it is an ancestor of another Text component. It controls what element and style to be rendered.
It would be beneficial to hook in to the same context in the application code. For example when building text components with custom styling using RNW’s Text component, it would make sense to apply some styles only on the parent and let the ancestor inherit.
In the following example the blue text doesn’t get large (because of the default style).
functionMyText({ large, blue, children }){return(<Textstyle={[styles.defaults,large&&styles.large,blue&&styles.blue]}>{children}</Text>);}conststyles=StyleSheet.create({defaults: {fontSize: 16,fontFamily: "Times"},large: {fontSize: 21},blue: {color: "blue"}});<MyTextlarge>
Go to
<MyTextbluehref="/page">
a page
</MyText></MyText>
I imagine using the TextAncestorContext like this:
function MyText({ large, blue, children }) {
const hasTextAncestor = React.useContext(TextAncestorContext);
return (
<Text style={[!hasTextAncestor && styles.defaults, large && styles.large, blue && styles.blue]}>
{children}
</Text>
);
}
I'm going to close this because a) React Native needs to make this change too, b) you can create your own Text context to workaround the issues for now.
Is there an existing request?
Describe the feature request
The Text component uses the context
TextAncestorContext
to determine if it is an ancestor of another Text component. It controls what element and style to be rendered.It would be beneficial to hook in to the same context in the application code. For example when building text components with custom styling using RNW’s Text component, it would make sense to apply some styles only on the parent and let the ancestor inherit.
In the following example the blue text doesn’t get large (because of the default style).
I imagine using the TextAncestorContext like this:
CodeSandbox
The text was updated successfully, but these errors were encountered: