developit / developit/unistore

How to access connected component method?

Open
#150 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
2.8k
Forks
134
PR merge metrics
No merged PRs in 30d

Description

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 (





);
}
}

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

const Index = props => {
return (


Hello World .. {props.count}

Abouts


);
};

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

export default connect(['count'])(Index);
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.