Style tnode.children with React Native Render HTML custom rendering?

692 Views Asked by At

I want to add a strike-through decoration to the #text-node-like children of a particular item we render using custom rendering.

But I'm not aware of any way to actually target the child #text-like-node and apply styles to it.

For example, we could wrap the TChildrenRenderer like below, but those styles will not be inherited by the Text component that TChildrenRenderer renders. So this works for some styles, like lineDecorationType: 'line-through', but not for things like color.

<Text style={{ color: 'red' }}>
    <TChildrenRenderer tchildren={tnode.children} />
</Text>

Is there any approach that would allow us to set color on the inner rendered text?


Edit: The method has to allow for conditional styling, so a top-level prop like defaultTextProps is out.

2

There are 2 best solutions below

8
adsy On

I think you can add textProps but only on the parent TDefaultRenderer. May be good enough in your use case (?).

<TDefaultRenderer textProps={{ style: { color: 'red' }}}

Or possibly, on <RenderHtml> add defaultTextProps={{ style: { color: 'red' }}}

0
Jules Sam. Randolph On

You could use renderChild prop from TChildrenRendererProps. Something like:

const renderRedChild = ({ childElement }) => React.cloneElement(childElement, {...childElement.props, style: [childElement.props.style, { color: "red" }]})
//...
<TChildrenRenderer tchildren={tnode.children} renderChild={renderRedChild} />