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

How to access connected component method? #150

Open
serge20 opened this issue Mar 25, 2019 · 2 comments
Open

How to access connected component method? #150

serge20 opened this issue Mar 25, 2019 · 2 comments

Comments

@serge20
Copy link

serge20 commented Mar 25, 2019

Hi,

I am trying to use unistore with next.js. It works great until a page has a static method getInitialProps that needs to be called.

The way next.js works is that there is an _app.js file that acts as a parent container and loads the page as a child.

That parent is responsible for calling the getInitialProps of the child page. When I wrap my page with connect()(Page); the parent is not able to call getInitialProps anymore as the component returns the new component created by the connect method. Just wondering if anyone has any idea on how to handle this?

//_app.js
import React from "react";
import App, { Container } from "next/app";
import createStore from "unistore";
import { Provider } from "unistore/react";

let store = createStore({ count: 0 });

class MyApp extends App {
  static async getInitialProps({ Component, ctx }) {
    let pageProps = {};
    //Never gets triggered when page is wrapped in connect()
    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx);
    }
    return {
      ...pageProps
    };
  }

  render() {
    const { Component, pageProps } = this.props
    return (
      <Provider store={store}>
        <Container>
          <Component {...pageProps} />
        </Container>
      </Provider>
    );
  }
}

export default MyApp;
//pages/index.js
import Link from "next/link";
import { connect } from "unistore/react";

const Index = props => {
  return (
    <div>
      Hello World .. {props.count}
      <Link href="/about">
        <a>Abouts</a>
      </Link>
    </div>
  );
};

Index.getInitialProps = () => {
  console.log(" CALL THIS");
  return {
    hello: 'bye'
  }
}

export default connect(['count'])(Index);
@josantana
Copy link

Just to help whoever that could be still struggling about this...

// pages/index.js
import { connect } from "unistore/react";

const UnwrappedIndex = ({ count, hello }) => (
  <div>
    {`${hello} world x ${count}!`}
  </div>
);

const Index = connect('count')(UnwrappedIndex);

Index.getInitialProps = () => {
  // Get some data...
  return {
    hello: 'Bye',
  };
}

export default Index;

Credits to @acjay.

@developit
Copy link
Owner

Hooks-based integrations for React and Preact should be coming shortly, which would be a nice fix for this since they don't wrap your component at all.

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

No branches or pull requests

3 participants