question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rendering rich text in React Native

See original GitHub issue

Thanks to rich-text-react-render, we can render Contentful rich text inside a React app. Moreover, some defaults are provided for DOM rendering (using tags like <p>, <h1> and so on).

However, it is not possible to render these tags in a React Native app, as React Native uses custom components, not DOM elements (for example <Text> for text).

Fortunately, rich-text-react-renderer makes possible to override the default components and allows to provide a custom renderer for each type of content.

Then, it is up to the user to provide their own React Native renderer.

Do you think it could be a good idea to provide a default renderer for React Native as well?

People might override it to use their own components for easier styling, but this could make it easier to get started using Contentful with React Native.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:15
  • Comments:23 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
Khaledgarbayacommented, Jul 2, 2019

Since the react-renderer is flexible enough I think we can have a package that just exports a renderer option to add the rich-text-react-renderer

Example:

import { documentToReactComponents } from '@contentful/rich-text-react-renderer';
import { reactNativeRenderer } from '@contentful/rich-text-react-renderer';

documentToReactComponents(document, reactNativeRenderer)
8reactions
neil-gebbie-smarterleycommented, Oct 1, 2020

For anyone looking for a solution, you can use this as a starting point. Your use case will be different based on where your using it, but for most people it’s enough to get started. I’ve edited the code inside the github comment box so if there is errors or formatting issues sorry about that.

const contentfulToReactnative = {
    renderMark: {
      [MARKS.UNDERLINE]: (text) => {
        return text;
      },
      [MARKS.BOLD]: (text) => {
        return <Text>{text}</Text>;
      },
      [MARKS.ITALIC]: (text) => {
        return <Text>{text}</Text>;
      },
      [MARKS.CODE]: (text) => {
        return <Text>{text}</Text>;
      },
    },
    renderNode: {
      [INLINES.HYPERLINK]: (node) => {
        return null;
      },
      [BLOCKS.EMBEDDED_ENTRY]: (node) => {
        return null;
      },
      [BLOCKS.PARAGRAPH]: (_node, children) => {
        return <Text>{children}</Text>;
      },
      [BLOCKS.EMBEDDED_ASSET]: (node) => {
        return null;
      },
      [BLOCKS.HEADING_1]: (_node, children) => <Text>{children}</Text>,
      [BLOCKS.HEADING_2]: (_node, children) => <Text>{children}</Text>,
      [BLOCKS.HEADING_3]: (_node, children) => <Text>{children}</Text>,
      [BLOCKS.HEADING_4]: (_node, children) => <Text>{children}</Text>,
      [BLOCKS.HEADING_5]: (_node, children) => <Text>{children}</Text>,
      [BLOCKS.HEADING_6]: (_node, children) => <Text>{children}</Text>,
      [BLOCKS.UL_LIST]: (_node, children) => {
        return (
          <View>
            {children.map((child, i) => {
              return child;
            })}
          </View>
        );
      },
      [BLOCKS.OL_LIST]: (_node, children) => {
        return children.map((child, i) => {
          return child;
        });
      },
      [BLOCKS.LIST_ITEM]: (_node, child) => {
        return <View>{child}</View>;
      },
      [BLOCKS.QUOTE]: (_node, child) => {
        return <Text>{child}</Text>;
      },
      [BLOCKS.HR]: (_node, child) => {
        return <Text>{child}</Text>;
      },
    },
};

And then you can do this:

import { documentToReactComponents } from '@contentful/rich-text-react-renderer'
documentToReactComponents(objFromContentful, contentfulToReactnative)
Read more comments on GitHub >

github_iconTop Results From Across the Web

@contentful/rich-text-react-renderer - npm
React renderer for the Contentful rich text field type. ... Start using @contentful/rich-text-react-renderer in your project by running `npm ...
Read more >
Implementing a Rich Text Editor in React Native - Medium
This tutorial will give you a head start in understanding the documentation of React Native Rich Text Editor better.
Read more >
Text & Rich Content - React Native Example for Android and iOS
WIP: Experiment to use HTML like markup to create stylized text in react-native. 09 September 2017. Render html string as react native custom...
Read more >
Handle Rich Text in React - Documentation - Prismic
Render Rich Text ... In your new React application, open slices/Text/index.js. This is the React component for your Text Slices. Your text is...
Read more >
Text - React Native
A React component for displaying text. Text supports nesting, styling, and touch handling. In the following example, the nested title and body ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found