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.

Can't dynamically render two Text components

See original GitHub issue

I want to put two separate <Text> elements in my footer with different styling, both of which need access to the totalPages, however for some reason only the first one is ever displayed.

Can reproduced on the page wrap example (https://react-pdf.org/repl?example=page-wrap) by replacing the single page number <Text> element with the following:

      <Text style={styles.pageNumber} render={({ pageNumber, totalPages }) => (
        `a ${pageNumber} / ${totalPages}`
      )} fixed />
      <Text style={[styles.pageNumber, {bottom: 0}]} render={({ pageNumber, totalPages }) => (
        `b ${pageNumber} / ${totalPages}`
      )} fixed />

I would expect to see the ‘b’ element at the bottom of the screen, with the ‘a’ above it, but I only see ‘a’

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

4reactions
Skywalker13commented, Sep 26, 2020

Alright, I just found in documentation that totalPages is available only in <Text> component. Here is a little workaround that I found, it’s not very readable but it works.

 <Text style={styles.pageNumber} render={({ pageNumber, totalPages }) => (
     <View>
          <Text> {`a ${pageNumber} / ${totalPages}\n`}</Text>
          <Text> {`b ${pageNumber} / ${totalPages}`}</Text>
     </View>
 )} fixed />

<View> is not really supported (I’ve experimented stuff like that and inspected the source code) in the render of <Text>. Just use a React.Fragment instead.

<Text style={styles.pageNumber} render={({ pageNumber, totalPages }) => (
   <>
        <Text> {`a ${pageNumber} / ${totalPages}\n`}</Text>
        <Text> {`b ${pageNumber} / ${totalPages}`}</Text>
   </>
)} fixed />
1reaction
zacajcommented, Sep 22, 2020

Ah, thank you. I didn’t think you were allowed to put a View instead of the Text render, but that was because it crashes if you’re using styled components for the view/text.

Read more comments on GitHub >

github_iconTop Results From Across the Web

I am unable to render components dynamically in react.js
So, I started working in react and got stuck, when I was trying to render a component dynamically into the view, from a...
Read more >
React Tutorial #4 - Dynamically Rendering Multiple Components
... try restarting your device. Your browser can't play this video. ... React Tutorial #4 - Dynamically Rendering Multiple Components.
Read more >
Dynamically Importing Components with React.lazy
In React, dynamically importing a component is easy—you invoke React.lazy with the standard dynamic import syntax and specify a fallback UI.
Read more >
Render Props - React
The term “render prop” refers to a technique for sharing code between React components using a prop whose value is a function. A...
Read more >
View - React Native
The most fundamental component for building a UI, View is a ... that wraps two boxes with color and a text component in...
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