gpt4 book ai didi

reactjs - 连接的组件未接收 Redux 中的 store props

转载 作者:行者123 更新时间:2023-12-03 13:41:05 26 4
gpt4 key购买 nike

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

我已经包含了相关的代码,我已经将我的 redux reducer 构造为 ducks格式,因此操作/创建者和缩减者位于一个模块文件中。

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

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

非常感谢您的帮助。

// 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')
);

最佳答案

登录组件中的 Prop 为空的原因是因为您实际上并未使用连接的登录容器,正如您在containers/login中提到的那样

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

从“/path/to/containers/Login”导入登录名;

关于reactjs - 连接的组件未接收 Redux 中的 store props,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48138715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com