Handling newline text nodes in React Native #2405
-
I am trying to use mdx in a React Native app, and I'm wondering how others have solved the problem where an empty line between two lines of text throws an error because text must be rendered with a Text component in React Native. For example, I can get this to render successfully, by passing # Heading 1 Likewise, I can get this to render successfully, by passing ## Heading 2 However, the following input throws the error # Heading 1
## Heading 2 This is because the line between the headings gets compiled to import {
Fragment as _Fragment,
jsx as _jsx,
jsxs as _jsxs,
} from 'react/jsx-runtime';
function _createMdxContent(props) {
const _components = {
h1: 'h1',
h2: 'h2',
...props.components,
};
return _jsxs(_Fragment, {
children: [
_jsx(_components.h1, {
children: 'Heading 1',
}),
'\\n',
_jsx(_components.h2, {
children: 'Heading 2',
}),
],
});
}
export default function MDXContent(props = {}) {
const {wrapper: MDXLayout} = props.components || {};
return MDXLayout
? _jsx(MDXLayout, {
...props,
children: _jsx(_createMdxContent, {
...props,
}),
})
: _createMdxContent(props);
} It's the To see this, clone my demo repo here: https://github.com/tsargent/mdx-poc. Install dependencies with Error with 2 lines in the mdx content: If you remove either line in demo.mdx, it renders successfully: The compilation is done in the metro config here: https://github.com/tsargent/mdx-poc/blob/main/metro.transformer.js#L9-L12 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Welcome @tsargent! 👋 MDX renders for web, and react-native doesn't fully support web content. |
Beta Was this translation helpful? Give feedback.
Welcome @tsargent! 👋
MDX renders for web, and react-native doesn't fully support web content.
You could try a wrapper like https://github.com/danieldunderfelt/rn-mdx which translates all the markdown supported HTML, into the
react-native
equivalents.