gpt4 book ai didi

javascript - 使用 Connect 进行 mapStateToProps 和 mapDispatchToProps 时对 redux : still getting empty object for this. props 使用react

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

我正在尝试按照 React Redux 上的本教程 ( https://medium.com/@stowball/a-dummys-guide-to-redux-and-thunk-in-react-d8904a7005d3 ) 进行操作,但当我打印出 this.props 时,得到一个空对象。似乎mapStateToProps实际上并没有设置this.props,因为react redux的connect没有被调用,但我不确定为什么

这就是状态应该是什么(就像教程中一样),我所做的就是将组件的名称 ItemList 更改为 Home

enter image description here

这是我得到的信息(没有任何内容映射到状态):

enter image description here

actions/items.js

export function itemsHasErrored(bool) {
return {
type: 'ITEMS_HAS_ERRORED',
hasErrored: bool
};
}

export function itemsIsLoading(bool) {
return {
type: 'ITEMS_IS_LOADING',
isLoading: bool
};
}

export function itemsFetchDataSuccess(items) {
return {
type: 'ITEMS_FETCH_DATA_SUCCESS',
items
};
}

export function itemsFetchData(url) {
return (dispatch) => {
dispatch(itemsIsLoading(true));

fetch(url)
.then((response) => {
if (!response.ok) {
throw Error(response.statusText);
}

dispatch(itemsIsLoading(false));

return response;
})
.then((response) => response.json())
.then((items) => dispatch(itemsFetchDataSuccess(items)))
.catch(() => dispatch(itemsHasErrored(true)));
};
}

组件/Home.js

export class Home extends Component {
componentDidMount() {
console.log(this.props)
}

render() {

if (this.props.hasErrored) {
return <p>Sorry! There was an error loading the items</p>;
}

if (this.props.isLoading) {
return <p>Loading…</p>;
}

return (
<ul>
{this.props.items.map((item) => (
<li key={item.id}>
{item.label}
</li>
))}
</ul>
);

}
}


const mapStateToProps = (state) => {
return {
items: state.items,
hasErrored: state.itemsHasErrored,
isLoading: state.itemsIsLoading
};
};

const mapDispatchToProps = (dispatch) => {
return {
fetchData: (url) => dispatch(itemsFetchData(url))
};
};

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

reducer /index.js

export function itemsHasErrored(state = false, action) {
switch (action.type) {
case 'ITEMS_HAS_ERRORED':
return action.hasErrored;

default:
return state;
}
}

export function itemsIsLoading(state = false, action) {
switch (action.type) {
case 'ITEMS_IS_LOADING':
return action.isLoading;

default:
return state;
}
}

export function items(state = [], action) {
switch (action.type) {
case 'ITEMS_FETCH_DATA_SUCCESS':
return action.items;

default:
return state;
}
}

商店/configureStore.js

import { applyMiddleware, createStore } from 'redux'
import thunk from 'redux-thunk'
import rootReducer from '../reducers'

export default function configureStore(initialState) {
return createStore(
rootReducer,
initialState,
applyMiddleware(thunk)
)
}

App.js

import React, { Component } from 'react'
import { Provider } from 'react-redux'
import configureStore from './store/configureStore'

import { Home } from './components/Home'
const store = configureStore()

export default class App extends Component {

render () {
return (
<Provider store={store}>
<Home />
</Provider>
)
}

}

index.js(我正在使用 create-react-app 样板)

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();

最佳答案

问题是您正在导出两个组件...未连接的组件(通过带有 export class Home... 的命名导出 Home)和已连接的组件(通过导出默认值)。然后,您导入并渲染未连接的组件:

import { Home } from './components/Home'

由于您想使用connected组件,因此您应该像这样导入default导出:

import Home from './components/Home'

您可能只想导出连接的组件,除非您有某种原因需要使用未连接的组件。

关于javascript - 使用 Connect 进行 mapStateToProps 和 mapDispatchToProps 时对 redux : still getting empty object for this. props 使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46288005/

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