gpt4 book ai didi

javascript - Redux 使用 combineReducers 返回空白页

转载 作者:行者123 更新时间:2023-11-30 11:37:08 28 4
gpt4 key购买 nike

当我使用定义的名称导入 reducers 时,我的应用程序工作正常,但是当我使用 combineReducers 时,它返回空白页。

store.js代码是:

import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import reducer from '../reducers/index';

const store = createStore(
reducer,
applyMiddleware(thunk)
);
var routes =(
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/" component={Main}>
<Route path="/testingtask" component={TestingTask}>
</Router>
</Provider>
);

TestingTask.js 是:

import React from 'react';
import { render } from 'react-dom';
import ReactDOM from 'react-dom';
import TaskContainer from '../containers/TaskContainer';



export default class TestingTask extends React.Component {

render() {
return (
<TaskContainer />
);
}
}
module.exports = TestingTask;

TaskContainer.js 是:

import React, {Component} from 'react';
import { Link } from 'react-router';
import ReactDOM from 'react-dom';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import {bindActionCreators} from 'redux';
import { fetchPostsWithRedux,fetchPosts,fetchPostsError,fetchPostsSuccess,fetchPostsRequest } from '../actions/TaskAction';

class TaskContainer extends React.Component {
componentDidMount(){
this.props.fetchPostsWithRedux()
}
render(){
return (
<ul>
{
this.props.posts &&
this.props.posts.map((post,i) =>{
return(
<li key={i}>{JSON.parse(post.form_data).title}</li>
)
})
}
</ul>
)
}
}


function mapStateToProps(state){
return {
posts: state.posts
}
}

function mapDispatchToProps(dispatch) {
return bindActionCreators({fetchPostsWithRedux: fetchPostsWithRedux}, dispatch)

}

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

TaskAction.js 是:

export function fetchPostsRequest(){
return {
type: "FETCH_REQUEST"
}
}

export function fetchPostsSuccess(payload) {
return {
type: "FETCH_SUCCESS",
payload
}
}

export function fetchPostsError() {
return {
type: "FETCH_ERROR"
}
}

export function fetchPostsWithRedux() {
return (dispatch) => {
dispatch(fetchPostsRequest());
return fetchPosts().then(([response, json]) =>{
if(response.status === 200){
dispatch(fetchPostsSuccess(json.data))
}
else{
dispatch(fetchPostsError())
}
})
}
}

export function fetchPosts() {
const URL = "api-url";
let user = JSON.parse(localStorage.getItem('user'));
return fetch(URL, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
api_token: user.api_token,
type: 'task',
})
}).then(response => Promise.all([response, response.json()]));
}

TaskReducer.js 是:

import {
FETCH_REQUEST,
FETCH_SUCCESS,
FETCH_ERROR
} from '../actions/TaskAction';

export default function TaskReducer (state = {}, action) {
switch (action.type) {
case "FETCH_REQUEST":
return state;
case "FETCH_SUCCESS":
return {...state, posts: action.payload};
default:
return state;
}
}

/reducers/index.js 是:

import { combineReducers } from 'redux';
import TaskReducer from './TaskReducer';

export default combineReducers({
TaskReducer,
})

请注意,当在 store.js 中使用 import reducer from '../reducers/TaskReducer'; 时,它工作正常。

最佳答案

combineReducers 函数将状态拆分为对象,您可以通过 reducer 的名称访问这些对象。

因此,这个函数:

function mapStateToProps(state){
return {
posts: state.posts
}
}

必须成为

function mapStateToProps(state){
return {
posts: state.TaskReducer.posts
}
}

查看 documentation举个例子:

Returns

(Function): A reducer that invokes every reducer inside the reducers object, and constructs a state object with the same shape.

如果您想为存储键定义不同的名称,只需更改传递给函数的对象即可:

export default combineReducers({
task: TaskReducer,
})

function mapStateToProps(state){
return {
posts: state.task.posts
}
}

关于javascript - Redux 使用 combineReducers 返回空白页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43996402/

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