连接的组件未收到商店props n Redux

IT技术 reactjs redux react-redux
2021-04-26 22:36:50

我正在做一些重构并尝试将更高级别的组件连接到 redux,connect()但我正在连接的组件一直给我空的props。

我已经包含了相关的代码,我已经将我的 redux 减速器组织成一种鸭子格式,所以动作/创建器和减速器都在一个module文件中。

这些文件是containers/login.jspresentation/login.jspresentation/logins.jsapp.js和根index.js

当我决定重命名一些动作、文件和减速器,将连接移动到更高的组件时,连接停止工作,现在我有空的props。

非常感谢帮助。

// containers/login.js
import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom'
import { fetchPage } from '../redux/modules/Login';
import Login from '../presentation/Login';

const mapStateToProps = (state) => {
  return {
    page: state.page,
    forms: state.forms
  }
}

const mapDispatchToProps = (dispatch) => {
  return {
      fetchPage: () => dispatch(fetchPage())
  } // here we're mapping actions to props
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(Login);

// redux/modules/login.js
import fetch from 'cross-fetch';

const RECIEVE_FORM = 'RECIEVE_FORM';

export const receiveForm = (response) => ({
  type: RECIEVE_FORM,
  forms: response.forms
})

const initialState = {
  page: "",
  forms: []
}

// MIDDLEWARE NETWORK REQUEST DISPATCHER
export const fetchPage = () => {
  return dispatch => {
    return fetch('http://localhost:3001/login')
    .then(
      response => response.json(),
    )
    .then(
      response => dispatch(receiveForm(response))
    )
  }
}

// REDUCER COMPOSITION CALL EXISTING REDUCERS
// REDUCER COMPOSITION PATTERN
// ACCUMULATIVE ACTION REDUCER
export default function Login(state = initialState, action){
  switch (action.type){
    case RECIEVE_FORM:
    return {
      ...state,
      forms: action.forms
    }
    default:
    return state;
  }
}

// presentation/login.js
import React, { Component } from 'react';
import styled from 'styled-components';
import Wrapper from '../components/Wrapper';
import Card from '../components/Card';
import Text from '../components/Text';
import Logo from '../components/Logo';
import FormGroup from '../components/FormGroup';


const WrapperLogin = styled(Wrapper)`
    .login__card{
        padding: 4.5rem 2.5rem 2rem 2.5rem;
    }
`;

const BoxLogo = styled.div`
    .login__logo{
        display: block;
        margin: 0 auto;
    }
`;

export default class Login extends Component{

  componentDidMount() {
    console.log(this.props)
    //this.props.fetchPage();
  }

    render(){
        return(
            <main>
                <WrapperLogin className="login">
                    <Card className="login__card">
                        <BoxLogo>
                            <Logo className="login__logo" width={187.36} height={76.77} />
                        </BoxLogo>
                        <FormGroup name="login" className="login_formGroup" />
                    </Card>
                    <Text primitive="p" margin='4px 0 0 0' size="0.8rem" textAlign="center" display='block'>Brought to you by WORLDCHEFS</Text>
                </WrapperLogin>
            </main>
        )
    }
}

// app.js
// manage routes here
//import _ from 'lodash';
import React, { Component } from 'react'
import { BrowserRouter as Router, Route, Redirect } from 'react-router-dom';
import { ThemeProvider } from 'styled-components';
import Login from './presentation/Login';

type Props = {

}

type State = {
  mode: string
};




export default class App extends Component <Props, State> {

  constructor(){
    super();
    this.state = {
      ...this.state,
      mode: 'mobile'
    }
  }

  render(){
    return(
      <ThemeProvider theme={{ mode: this.state.mode }}>
        <Login />
      </ThemeProvider>
    )
  }

}

     // root
    import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import configureStore from './redux/configureStore';
import registerServiceWorker from './registerServiceWorker';
import App from './App';
import { injectGlobal } from 'styled-components';
import styles from './assets/styles';

const store = configureStore();
     ReactDOM.render(
        <Provider store={store}>
        <App />
        </Provider>
        , document.getElementById('root')
    );
2个回答

您在 Login 组件中的props为空的原因是因为您实际上并未使用连接的 Login 容器,正如您在 containers/login

因此,在您的 App.js 中,将 login 的导入更改./presentation/login

import Login from '/path/to/containers/Login';

您在 app.js 中导入了演示组件,而不是容器组件。请导入您的容器组件,如下所示

import Login from './containers/login.js';

根据我对您代码的理解,这将解决问题