Easy-peasy: How to use in a "normal" statefull class component?

Created on 20 Feb 2019  路  5Comments  路  Source: ctrlplusb/easy-peasy

Like the subject,

how to access global state inside a stefaull class component?

i can't use useState because:
Hooks can only be called inside the body of a function component.

Most helpful comment

hi

I want use with react-redux

so It's like code is it right?

import { StoreProvider, createStore } from 'easy-peasy';
import { Provider } from 'react-redux'; // 馃憟 import the provider
import model from './model'

const store = createStore(model);

const App = () => (
  <StoreProvider store={store}>   // 馃憟 StoreProvider
      <Provider store={store}>  // 馃憟 Provider
        <TodoList />
      </Provider>
  </StoreProvider>
)

All 5 comments

I think the options are either:

  • try to use only functional components
  • or also use react-redux (https://github.com/ctrlplusb/easy-peasy#alternative-usage-via-react-redux)

Yep @sibartlett is right. You can use react-redux with this lib.

hi

I want use with react-redux

so It's like code is it right?

import { StoreProvider, createStore } from 'easy-peasy';
import { Provider } from 'react-redux'; // 馃憟 import the provider
import model from './model'

const store = createStore(model);

const App = () => (
  <StoreProvider store={store}>   // 馃憟 StoreProvider
      <Provider store={store}>  // 馃憟 Provider
        <TodoList />
      </Provider>
  </StoreProvider>
)

You are correct @hanquf1

@hanquf1 How we can dispatch easy-peasy actions??

Was this page helpful?
0 / 5 - 0 ratings