Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Context not handled correctly (react-redux and react-intl) #522

Open
ribx opened this issue Mar 11, 2019 · 18 comments
Open

React Context not handled correctly (react-redux and react-intl) #522

ribx opened this issue Mar 11, 2019 · 18 comments

Comments

@ribx
Copy link

ribx commented Mar 11, 2019

Describe the bug
I am using react-redux and react-intl, which both use a provider component and react's context.

When I use a component, in this case on that is connected to the redux store, I get the following error:

Invariant Could not find "store" in the context of "Connect(Compoment)". Either wrap the root component in a , or pass a custom React context provider to and the corresponding React context consumer to Connect(Compoment) in connect options.

Found an issue with react-router, which could be important here:

If you use React Router, something like {() => routes} won’t work. Due to the way context works in React 0.13, it’s important that the children are created inside that function. Just referencing an outside variable doesn’t do the trick. Instead of {() => routes}, write {createRoutes} where createRoutes() is a function that actually creates (and returns) the route configuration.

To Reproduce
Steps to reproduce the behavior including code snippet (if applies):

  1. use a component that uses react's context within react-pdf components
  2. exception is thrown

(I have no time to create a minimal code snippet now, but could do so if someone needs it)

Expected behavior
react-pdf should be able to render components, that rely on reacts context api

Desktop (please complete the following information):

  • OS: linux, Gentoo Linux
  • Browser: chrome and firefox
  • React-pdf/renderer version: v1.4.0
@ribx ribx changed the title React Context not correctly handled (react-redux and react-intl) React Context not handled correctly (react-redux and react-intl) Mar 11, 2019
@diegomura
Copy link
Owner

A code snippet to replicate this would be great

@ribx
Copy link
Author

ribx commented Mar 16, 2019

https://github.com/ribx/react-pdf-test

import React, {Component} from 'react'
import {createStore} from 'redux'
import ReactDOM from 'react-dom'
import {IntlProvider, FormattedMessage} from 'react-intl'
import {Provider as ReduxProvider, connect} from 'react-redux'
import {Document, Page, View, Text, PDFViewer} from '@react-pdf/renderer'


const store = createStore(state => state)

const Connected = connect(state => ({state}))(props => console.log('state', props.state) || props.children)

class App extends Component {
  render() {
    return (
      <div className="App">
        <PDFViewer>
          <Document>
            <Page>
              <View>
                <Text>
                  <FormattedMessage id="test">{s => s}</FormattedMessage>
                </Text>
              </View>
              <View>
                <Text>
                  <Connected>
                    Redux connected component Test
                  </Connected>
                </Text>
              </View>
            </Page>
          </Document>
        </PDFViewer>
      </div>
    )
  }
}

ReactDOM.render(
  <ReduxProvider store={store}>
    <IntlProvider locale="en" messages={{en: {id: "test", defaultMessage: "React PDF Test"}}}>
      <App/>
    </IntlProvider>
  </ReduxProvider>,
  document.getElementById('root'),
)

I think this is somehow connected to how the new context is working, but I have still problems understanding the concept of react-reconciler.

@no-response no-response bot removed the incomplete label Mar 16, 2019
@yjose
Copy link

yjose commented Jul 24, 2019

Hi @diegomura, any idea how we can solve this problem. Using context API is a common case.

@sirgalleto
Copy link

sirgalleto commented Sep 11, 2019

Hi, just want to mention that I'm having also problems connecting this component to some context, either the Redux one, as well as other used in my current project.

Is there something we can do in order to help you?

cc @diegomura

@zeabdelkhalek
Copy link

same issue here, can't inject react intl ... an alternative solution is to connect the parent component and pass data into props but still alternative.

@Nases
Copy link

Nases commented Dec 1, 2020

Having same issue on v1.6.12. Can't use react context API.

@hotcakedev628
Copy link

I am having the similar issue.

Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider>

Some of my code for React PDF component are following.

import React from 'react';
import PropTypes from 'prop-types';
import {
  Document,
  Page,
  View,
  Text,
  Image,
  StyleSheet,
  Link
} from '@react-pdf/renderer';
import { useDispatch, useSelector } from 'src/store';

const InvoicePDF = ({ data }) => {
  const dispatch = useDispatch();
  const { invoice } = useSelector((state) => state.invoice);

  return (
    <Document>
      ...
    </Document>
  )
}

export default InvoicePDF;

package.json is the following.

    "@react-pdf/renderer": "^1.6.10",
    "@reduxjs/toolkit": "^1.4.0",

@SrividyaKK
Copy link

I am having the same issue. Any update regarding the same?

@hotcakedev628
Copy link

I am having the same issue. Any update regarding the same?

@SrividyaKK It's still silent.

@potofpie
Copy link

potofpie commented Feb 3, 2021

I see there is a version 2 branch. Has anyone tried this in yet? @SrividyaKK Or have the contributors add this yet? @diegomura

@SrividyaKK
Copy link

I see there is a version 2 branch. Has anyone tried this in yet? @SrividyaKK Or have the contributors add this yet? @diegomura

Nope. Not me.

@diegomura diegomura added the bug label Apr 5, 2021
@diegomura diegomura pinned this issue Apr 9, 2021
@joaopedrocoelho
Copy link

I'm using the useContext hook and I always get the value as undefined, any workaround?

@dluigirafael
Copy link

dluigirafael commented Apr 13, 2021

Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider>

<PDFViewer>
    <LayoutReturn />
</PDFViewer>

...

LayoutReturn  : 
<Document>
    <Layouts layoutsBackgrounds={layoutsBackgrounds} setLayoutsBackgrounds={setLayoutsBackgrounds} />
</Document>

Layouts :
<Page size="A4" style={{ backgroundColor: "tomato" }}>
          {layoutsBackgrounds[1] ? (
            <View
              style={{ color: "white", textAlign: "center", margin: 30 }}
              onClick={() => {
                setNewBg(1);
              }}
            >
              <Text>Img</Text>
            </View>
          ) : (
            <View
              style={{ color: "black", textAlign: "center", margin: 30 }}
              onClick={() => {
                setNewBg(1);
              }}
            >
              <Text>No img</Text>
            </View>
          )}
        </Page>

Same issue

@dluigirafael
Copy link

image
same with useContext

@diegomura
Copy link
Owner

This is an issue on the React side unfortunately. There's an open ticket for awhile now facebook/react#17275. I'll try to reactivate that

@walosha
Copy link

walosha commented Sep 6, 2021

same issue here, can't inject react intl ... an alternative solution is to connect the parent component and pass data into props but still alternative.

I solved the issue by lift up the state to the parent component. thanks

@mtullo27
Copy link

Hello, I am still having this issue has anyone come to a solution or work around?

@markcnunes
Copy link

I am not using react-redux or react-intl but here is what worked for a custom provider I wanted to access within a Document while using PDFViewer.

import { CustomProvider } from './CustomProvider';
import {
  PDFViewer as PDFViewerOriginal,
  PDFViewerProps,
} from '@react-pdf/renderer';

export const PDFViewer = ({ children, ...props }: PDFViewerProps) => {
  return (
    <PDFViewerOriginal {...props}>
      <CustomProvider>
        {/**
         * Due to a known issue with React, Contexts are not accessible by children of the 'react-pdf' PDFViewer.
         * Because 'react-pdf' is a custom renderer and current React limitations, we have to "bridge" contexts.
         * We need to subscribe to a context from within the PDFViewer and "bridge" the context by creating a Provider as a
         * child of the PDFViewer.
         *
         * For more info read this: @link https://github.com/diegomura/react-pdf/issues/522#issuecomment-861545047
         */}
        {children}
      </CustomProvider>
    </PDFViewerOriginal>
  );
};

Here is a possible solution if your provider is more complex and needs to access some data:

import { CustomProvider, CustomContext } from './CustomProvider';
import {
  PDFViewer as PDFViewerOriginal,
  PDFViewerProps,
} from '@react-pdf/renderer';
import { useContext } from 'react';

export const PDFViewer = ({ children, ...props }: PDFViewerProps) => {
  const customContext = use context(CustomContext);
  return (
    <PDFViewerOriginal {...props}>
      <CustomProvider value={customContext}>
        {children}
      </CustomProvider>
    </PDFViewerOriginal>
  );
};

You can read more about this solution here:
facebook/react#17275 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests