简体   繁体   English

无法从mapStateToProps访问更新的状态,未定义

[英]Cannot access updated state from mapStateToProps, undefined

I'm new to redux and am having trouble accessing state from mapStateToProps. 我是Redux的新手,无法从mapStateToProps访问状态。 I'm trying to create a 'folder' when the user enters the folder name and submits. 当用户输入文件夹名称并提交时,我正在尝试创建“文件夹”。 I've managed to update the state with an array of folder names but can't manage to access the folder array and use this to create my Folder components. 我已经设法用文件夹名称数组更新状态,但是无法管理访问文件夹数组并使用它来创建我的Folder组件。

Here is a container component that is supposed to give access to 'folders' in my Folders component: 这是一个容器组件,应该可以访问“文件夹”组件中的“文件夹”:

import { connect } from 'react-redux';
import Folders from './Folders';

const mapStateToProps = state => {
    return {
        folders: state.folders
    }
}

const Cabinet = connect(
    mapStateToProps
)(Folders);

export default Cabinet;

Here is the component im trying to access the state from: 这是组件试图从中访问状态的组件:

import React from 'react';
import Folder from './Folder';
import AddFolderButton from './AddFolderButton';

const Folders = ({ folders }) => (

            <div className="Folders">
                <h2>Folders</h2>
                <div className="Drawer">
                    {console.log(folders)}
                    <AddFolderButton /> 
                </div>
            </div>
)

export default Folders;

'folders' is always undefined when I update data in the store. 更新商店中的数据时,“文件夹”始终是未定义的。 I'm not quite sure what I'm doing wrong, I've been working through the basics tutorial in the Redux docs but think I may be fundamentally misunderstanding something. 我不太确定自己在做什么错,我一直在研究Redux文档中的基础教程,但是认为我可能从根本上误会了某些东西。

Here's the code I used to update the store: 这是我用来更新商店的代码:

Reducer 减速器

import { combineReducers } from 'redux';

const initialState = {
    folders: []
}

function handleFolders(state = initialState, action) {
    switch(action.type) {
        case 'CREATE_FOLDER':
            return {
                ...state, folders: [
                    ...state.folders,
                    {
                        name: action.name
                    }
                ]
            }
        default:
            return state;
    }
}

let rootReducer = combineReducers({
    handleFolders
})

export default rootReducer;

The button to 'create' a folder: “创建”文件夹的按钮:

class AddFolderButton extends React.Component {
    constructor() {
        super();
        this.state = {
            isClicked: false,
        };
        this.handleClick = this.handleClick.bind(this);
        this.handleOutsideClick = this.handleOutsideClick.bind(this);
        this.textInput = null;
    }

    handleClick() {
        if(!this.state.isClicked) {
            document.addEventListener('click', this.handleOutsideClick, false);
        } else {
            document.removeEventListener('click', this.handleOutsideClick, false);
        }
        this.setState(prevState => ({
            isClicked: !prevState.isClicked
        }));
    }

    handleOutsideClick(e) {
        if(this.node.contains(e.target)) {
            return;
        }
        this.handleClick();
    }

    render() {    
        return(
            <div className="new-folder-input" ref={ node => {this.node = node;}}>
                {!this.state.isClicked && (
                <button className="add-folder-btn" onClick={this.handleClick} >
                    <FontAwesomeIcon icon={faPlus} size="4x"/>
                </button>
                )}
                {this.state.isClicked && (
                    <div className="folder-input">
                        <form onSubmit={e => {
                            e.preventDefault()
                            this.props.createFolder(this.textInput.value)
                            this.handleClick();
                        }}
                        >
                            <input ref={node => this.textInput = node} 
                                type="text"
                                value={this.state.value}
                                autoFocus 
                                placeholder="Enter folder name" />
                            <button type="submit">Add Folder</button>
                        </form>
                    </div>
                )}
            </div>
        );
    }
}

const mapDispatchToProps = dispatch => ({
    createFolder: name => dispatch(createFolder(name))
})

export default connect(
    null,
    mapDispatchToProps
)(AddFolderButton);

Try changing your mapStateToProps method to this: 尝试将mapStateToProps方法更改为此:

const mapStateToProps = ({handleFolders: {folders}}) => ({
    folders
});

When your're combining your reducers you must access them first using it's name. 当您合并减速器时,必须首先使用它的名称访问它们。 Your reducer is called handleFolders and it has property folders 减速器称为handleFolders ,它具有属性folders

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM