wojtekmaj / react-pdf

Display PDFs in your React app as easily as if they were images.

Home Page:https://projects.wojtekmaj.pl/react-pdf

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"Invalid hook call" when using dynamic content

Rudomitori opened this issue · comments

Before you start - checklist

  • I followed instructions in documentation written for my React-PDF version
  • I have checked if this bug is not already reported
  • I have checked if an issue is not listed in Known issues
  • If I have a problem with PDF rendering, I checked if my PDF renders properly in PDF.js demo

Description

I use react contexts to reduce props drilling and it worked fine until I tried to use dynamic content.
When I use a component with useContext inside of prop render, I get error Invalid hook call

Steps to reproduce

REPL (Note: for some reason, you need to reload the page to see my code)

const styles = StyleSheet.create({
  page: { padding: 60 },
});

const CustomContext = React.createContext();

const Component = () => {
  const contextValue = React.useContext(CustomContext);
  
  return <Text>{contextValue}</Text>;
}

const doc = (
  <Document>
    <CustomContext.Provider value={55}>
      <Page style={styles.page} size="A4" wrap>
        <Component/> {/* Works fine */}
        <Text render={() => <Component/>} fixed /> {/* Throws error */}
      </Page>
    </CustomContext.Provider>
  </Document>
);

ReactPDF.render(doc);

Expected behavior

React contexts can be used in dynamic content

Actual behavior

Error Invalid hook call is thrown

Additional information

No response

Environment

  • Browser: Microsoft Edge | 123.0.2420.97 (Official build) (64-bit)
  • React-PDF version: 3.4.2
  • React version: 18.2.0

I'm lost.

The issue is related to the other repo and there is already issue about it
diegomura/react-pdf#2392