我正在做一些重构,并尝试使用connect()将更高级别的组件连接到redux但是我正在连接的组件一直给我空道具. 我已经包含了相关的代码,我将我的redux reducer组织成ducks格式,因此actions / crea
我已经包含了相关的代码,我将我的redux reducer组织成ducks格式,因此actions / creators和reducers在一个模块文件中.
这些文件是containers / login.js,presentation / login.js,presentation / logins.js,app.js和root index.js.
当我决定重命名某些操作,文件和缩减器时,将连接移动到更高的组件,连接停止工作,现在我有空道具.
非常感谢.
// 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') );你在登录组件中的道具是空的原因是你实际上没有使用连接的登录容器,因为你在容器/登录中提到它
因此,在您的App.js中,将登录名从./presentation/login更改为
import从’/ path /到/ containers / Login’登录;