Redux don't render the content of store

react redux
redux tutorial
redux store
redux toolkit
why redux is bad
redux example
redux reducer
redux-saga

I'm trying to render the store's value on view, but it don't render. I don't know why it is not rendering. Can someone explain me why ? (I have watched Dan Abramov's course on Egg Head and I still not understanding)

import React, { Component } from 'react';

import ReactDOM from 'react-dom';

import { createStore } from 'redux';

const reducer = (state = 0, action) => {
  switch(action.type) {
    case 'INCREMENT':
      console.log('Increment', state);

      return ++state;

    case 'DECREMENT':
      console.log('Decrement', state);

      return --state;

    default:
      return state;
  }
} 

const store = createStore(
  reducer
)

class App extends Component {
  render() {
    return (
      <div>
        <p>{JSON.stringify(this.props.store)}</p>

        <button onClick={() => store.dispatch({type: 'INCREMENT'})}>+</button>

        <button onClick={() => store.dispatch({type: 'DECREMENT'})}>-</button>
      </div>
    );
  }
}

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

Use Redux Connect to access state in component

ex:

export default connect(mapStateToProps, actions)(yourComponent);

Reducers, Remember that actions only describe what happened, but don't describe how the application's state changes. #Designing the State Shape. In Redux, all the  Redux assumes that you never mutate the objects it gives to you in the reducer. Every single time, you must return the new state object. Even if you don't use a library like Immutable, you need to completely avoid mutation. Immutability is what lets react-redux efficiently subscribe to fine-grained updates of your state.

import React, { Component } from 'react';

import ReactDOM from 'react-dom';

import { createStore } from 'redux';

const reducer = (state = 0, action) => {
  switch(action.type) {
    case 'INCREMENT':
      console.log('Increment', state);

      return ++state;

    case 'DECREMENT':
      console.log('Decrement', state);

      return --state;

    default:
      return state;
  }
} 

const store = createStore(
  reducer
);

class App extends Component {
  update() {
      this.forceUpdate();
  }
  componentDidMount(){
    this.unsubscribe = store.subscribe(this.update.bind(this));
  }
  componentWillUnmount(){
      this.unsubscribe();
  }
  render() {
    return (
      <div>
        <p>{JSON.stringify(store.getState())}</p>

        <button onClick={() => store.dispatch({type: 'INCREMENT'})}>+</button>

        <button onClick={() => store.dispatch({type: 'DECREMENT'})}>-</button>
      </div>
    );
  }
}


export default App;

Troubleshooting, Don't do this! Redux assumes that you never mutate the objects it gives to you in the reducer. Every single time, you must return the new state object. Even  Redux is a pattern for managing application state. If you do not have problems with state management, you might find the benefits of Redux harder to understand. Some UI libraries (like React) have their own state management system.

check this

import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import reduxThunk from 'redux-thunk';

import reducers from './reducers';
const store = createStore(
  reducers,
  applyMiddleware(reduxThunk)// not needed
);
ReactDOM.render(
  <Provider store={store}>  
      <App/> 
  </Provider>,
  document.querySelector('#root')
);

App component

import React, { Component } from 'react';
import { connect } from 'react-redux';
import * as actions from '../../actions';

class Signout extends Component {
  componentDidMount() {
    console.log(this.props.data);
  }

  render() {
    return <div>Sorry to see you go</div>;
  }
}
function mapStateToProps(state) {
  return { data: state.data};
}

export default connect(mapStateToProps, actions)(App);

Actions, You don't have to define action type constants in a separate file, or even to define them at all. For a small project, it might be easier to just use string literals for  Redux is a mechanized, humanoid android that was once destroyed. Judged from his visible, mechanic features (such as lines across his face), glasses, and golden hands made out metal and description, he was likely revived from a past outfit we all knew. Suggestions include Diecast, A.I.M., Sentinel, or Rebel.

You shouldn't mutate the state directly. Instead do like this:

return state + 1; // instead of ++state
return state - 1; // instead of --state

To render the state:

class App extends Component {
  render() {
    return (
      <div>
        <p>{store.getState()}</p>

To get the changes of the store, you will also need to subscribe for the changes:

const unsubscribe = store.subscribe( // generally, this is called on event handler
  ReactDOM.render(
    <App store={store} />,
    document.getElementById('root')
  )
)
unsubscribe()

You may also subscribe the changes in componentDidMount and unsubscribe in the componentWillUnmount lifecycle hook. You may look at the answer provided by henok tesfaye: subscribing and unsubscribing in lifecycle.

You can read it from the docs to know more about it.


But in general, we don't need subscriber. We can get the changes through mapping the states using connect. This blog explains it very well. So, I would suggest you to look at Usage with react

You Might Not Need Redux - Dan Abramov, Finally, don't forget that you can apply ideas from Redux without using Redux. For example, consider a React component with local state:  Exodus Redux is a newer Kodi addon that gives you access to tons of movies and shows. It looks a lot like the very popular Exodus Kodi addon (also called V8) and even has the same logo. But, after having used both I have drawn some comparisons and believe Exodus Redux is better than the Exodus addon.

Do React Hooks Replace Redux? - JavaScript Scene, You don't always need Redux for every app, or every component. If your app consists of a single view, doesn't save or load state, and has no  Redux is a predictable state management library and architecture which easily integrates with React. The primary selling points of Redux are: Deterministic state resolution (enabling deterministic

When (and when not) to use Redux, After all, don't components have their state? Why would you need a tool to help you manage that state? Don't get me wrong; React is great alone. Downloads for GTA 5 Redux available below. GTA 5 Redux is single-player only. ScriptHook will prevent you from playing online if you try, this will prevent you from accidentally getting banned. Downloads for Redux are currently high due to GTA V being free on the Epic Store. If you are…

Leveling Up with React: Redux, With Redux involved, components don't communicate directly between each other, but rather all state changes must go through the single source of truth, the  React-Redux is the official Redux UI binding library for React. If you are using Redux and React together, you should also use React-Redux to bind these two libraries. While it is possible to write Redux store subscription logic by hand, doing so would become very repetitive. In addition, optimizing UI performance would require complicated logic.

Comments
  • I had try it (and Provider) before, but it don't work.
  • you need to use comibeReducer like export default combineReducers({ data: dataReducer });