Iam new to learning Redux. When i write store.subscribe(render) my app compiles without errors but nothing shows up. I've been following an article(https://medium.freecodecamp.org/understanding-redux-the-worlds-easiest-guide-to-beginning-redux-c695f45546f6) that beautifully explains redux but that subscribe part is just not working. Any help in this regard will be very much appreciated.

I tried everything i could i even rebuilt the app but no luck.

index.js

./reducer/index.js

./store/index.js

App.js

3 Answers

0
Jay On Best Solutions

First, try changing line 17 of App.js from

<HelloWorld tech={this.state.tech}/>

to:

<HelloWorld tech={store.getState().tech}/>

Else, try putting a constructor into your App component:

constructor(props) {
    super(props);
    store.subscribe(() => {
      this.setState({
        tech: store.getState().tech
      });
    });
  }
0
Murtaza Hussain On

You need to add a provider into your store to subscribe the changes. For more details please check this link https://react-redux.js.org/api/provider

Please find the sample code for the store.

import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'

import { App } from './App'
import createStore from './createReduxStore'

const store = createStore()

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
)

0
tim On

You have to import createStore.

import { createStore } from 'redux';

Then you will need to make the store variable and pass in the reducer.

const store = createStore(yourReducer);

Then your reducer will look something like this.

const initState = {

}

const yourReducer = (state = initState, action) => {
    return state;
}

export default yourReducer