X
X
xXRustamXx2018-11-23 13:27:16
JavaScript
xXRustamXx, 2018-11-23 13:27:16

Why are props not inherited via the redux connect method in the App component?

const state = {
  desks: [
    {
      id: 1,
      name: 'Что нужно сделать'
    },
    {
      id: 2,
      name: 'В процессе'
    }
  ]
};

function desksReducer(state = [], action) {
  switch ( action.type ) {
    case 'ADD_DESK':
      return [
        ...state,
        action.desk
      ]
    default: 
      return state;
  }
}

function reducer(state = {}, action) {
  return {
    desks: desksReducer(state.desks, action)
  };
}

const store = Redux.createStore(reducer, state);

class App extends React.Component {
  constructor(props) {
    super(props);
    console.log(this.props);
  }

  render() {
    return (
      <div class="desk__items">
        <button>Add new desk</button>
      </div>
    );
  }
};

ReactRedux.connect(store)(App);

var Provider = ReactRedux.Provider;
ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>, 
  document.querySelector('#root'));

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2018-11-23
@xXRustamXx

/* ... */

const mapStateToProps = state => ({
  desks: state.desks,
});

const ConnectedApp = ReactRedux.connect(mapStateToProps)(App);

ReactDOM.render(
  <ReactRedux.Provider store={store}>
    <ConnectedApp />
  </ReactRedux.Provider>, 
  document.querySelector('#root'),
);

M
Mikhail Osher, 2018-11-23
@miraage

Because you do not read the documentation, in which everything is described in detail.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question