S
S
Sergey Alekseev2018-02-24 18:06:12
React
Sergey Alekseev, 2018-02-24 18:06:12

React + Redux, why is no data being passed?

Good afternoon, no data is being transmitted, I'm doing everything right like, but I'm not receiving data:

Создание хранилища и рендеринг компонента
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { ListProject } from "./Components/Projects";
import { rootReducer } from "./Reducers/reducers";
import { createStore } from "redux";

export const store = createStore(
  rootReducer, 
  window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);

ReactDOM.render(
  <Provider store={store}>
        <ListProject />
    </Provider>,
    document.getElementById("root")
);

Редусер
const initialState = {
  projects: ["Projects 001"]
};

export function rootReducer(state = initialState) {
  	return state;
}

Сам компонент с конектом
import React, { Component } from "react";
import { connect } from "react-redux";

export const ListProject = (props) => {
  console.log(props.projects);
    return <div>Привет из App, { props.projects }!</div>
}

const mapStateToProps = (state) => {
  projects: state.projects
}

export default connect(mapStateToProps)(ListProject);

I looked at many examples everywhere they transmit data in the connect method, but it doesn’t work for me, what could be the reason? The repository is created, the reducer works.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-02-24
@serj2000

Fix:

const mapStateToProps = (state) => {
  projects: state.projects
}

on the:
const mapStateToProps = (state) => ({
  projects: state.projects
});

The JavaScript parser, in your case, does not recognize curly braces as a function return object, but as the body of a function block that returns nothing. To return an object in short notation, it must be enclosed in parentheses.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question