简体   繁体   English

如何使用 React 和 Redux 将 map 分派到递归嵌套组件中的道具?

[英]How do I map dispatch to props within a recursively nested component using React and Redux?

I am attempting to make an application that displays a family tree to React using Redux.我正在尝试使用 Redux 制作一个显示家族树的应用程序。 Each person is displayed in a box with lines leading to their parents.每个人都显示在一个盒子里,盒子里有通往他们父母的线。 I want to have a feature where if you click on a person's 'card' you see a popup that displays further biographical information.我想要一个功能,如果你点击一个人的“卡片”,你会看到一个显示更多传记信息的弹出窗口。 The trouble I am having is about mapping the dispatch action within any component further down the tree than the root level person.我遇到的麻烦是将调度操作映射到树下比根级别人员更远的任何组件中。 It works as expected when I click on "John" at the top, but any of his ancestors and it breaks.当我点击顶部的“约翰”时,它按预期工作,但是他的任何祖先都坏了。 Hopefully, I'm just missing something obvious!希望我只是遗漏了一些明显的东西!

这是没有生物卡的家谱

这是单击家庭成员后的预期结果(带有适当的信息)

这是我收到的错误消息

Here are the components:以下是组件:

import React, {Component} from 'react';
import {connect} from 'react-redux';
import {getFamily} from '../redux/family.actions'
import "./Family.css";
import Person from './Person';

class Family extends Component {
    render() {
        const {rootPerson} = this.props;
        return (
            <div className="family family-container">
                <ul>
                    <Person key={rootPerson.id} info={rootPerson}/>
                </ul>
            </div>
        )
    }
}

const mapStateToProps = (state) => ({
    rootPerson: state.family.allFamilyMembers[0]
});

const mapDispatchToProps = dispatch => ({
    getFamilyFromStore: () => dispatch(getFamily())
});

export default connect(mapStateToProps, mapDispatchToProps)(Family);
import React from 'react';
import {connect} from 'react-redux';

import {selectPerson} from '../redux/family.actions'

import "./Person.css";

const Person = props => (
    <li className="oc-hierarchy" onClick={() => props.selectPerson(props.info)}>
        <div className="card card-body oc-node">
            <h1>{props.info.name}</h1>
        </div>

        {props.info && props.info.parents && props.info.parents.length > 0 && (
                <ul>
                    {props.info.parents.map(child => (
                        <Person info={child} />
                        ))}
                </ul>
            )} 
    </li>
);

const mapDispatchToProps = dispatch => ({
    selectPerson: (person) => dispatch(selectPerson(person))
});

export default connect(null, mapDispatchToProps)(Person);
import React from 'react';
import {connect} from 'react-redux';

import './App.css';

import Family from './components/Family';
import PersonBio from './components/PersonBio';

function App({selectedPerson}) {

  return (
    <div className="App">
        <Family />
        {selectedPerson == null ? null : <PersonBio />}
    </div>
  );
}

const mapStateToProps = (state) => ({
    selectedPerson: state.family.selectedPerson
});

export default connect(mapStateToProps) (App);
export const selectPerson = (person) => ({
    type: 'SELECT_PERSON',
    payload: person
})
const INITIAL_STATE = {
    allFamilyMembers: [
        {
            id:0,
            name: 'John',
            parents: [
                {
                    id:1,
                    name: 'Bill',
                    parents: [
                        {
                            id:3,
                            name: 'Bob',
                            parents: [
                            ]
                        },
                        {
                            id:4,
                            name: 'Barb',
                            parents: [
                            ]
                        },
                    ]
                },
                {
                    id:2,
                    name: 'Mary',
                    parents: [
                        {
                            id:5,
                            name: 'Matt',
                            parents: [
                                {
                                    id:7,
                                    name: 'Mark',
                                    parents: [
                                    ]
                                },
                                {
                                    id:8,
                                    name: 'Ruth',
                                    parents: [
                                    ]
                                },
                            ]
                        },
                        {
                            id:6,
                            name: 'Marge',
                            parents: [
                            ]
                        },
                    ]
                }
            ]
        }
    ],
    selectedPerson: null
}

const familyReducer = (state = INITIAL_STATE, action) => {
    switch (action.type) {
        case 'GET_FAMILY':
            return state;
        case 'SELECT_PERSON':
            return {
                ...state,
                selectedPerson: action.payload
            };
        default:
            return state;
    }
}

export default familyReducer;

Actually, you don't know the fundamental of ReactJS, you never passed the selectPerson function to the Person component, pay attention to this part of your code:实际上,您并不了解 ReactJS 的基本原理,您从未将selectPerson function 传递给Person组件,请注意这部分代码:

<Person key={rootPerson.id} info={rootPerson}/>

You should pass the selectPerson to this line.您应该将selectPerson传递给这一行。 otherwise, you got it as undefined inside the Person component否则,您在Person组件中将其视为undefined

<Person key={rootPerson.id} info={rootPerson} selectPersion={() => {}} />

Now the selectPerson is declared and you got a noOp function inside your Person component instead of getting undefined .现在声明了selectPerson并且您在Person组件中得到了noOp function 而不是undefined this is cause of your issue.这是您的问题的原因。

暂无
暂无

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

相关问题 我如何从另一个不是反应组件的文件中调用 useReducer 调度 function? (不使用 Redux) - How do i call a useReducer dispatch function from another file, which is not a react component? (without using Redux) 组件内嵌套对象内部的React-Redux访问道具 - React-Redux access props inside a nested object within component 如何访问 React props 中的嵌套对象属性 - How do I access nested object properties within React props 在功能组件中,如何使用 react-redux 连接从 redux 商店访问道具? - In a Functional Component, how do you access props from the redux store using react-redux connect? 如何优化React + Redux中嵌套组件的道具的小更新? - How to optimize small updates to props of nested component in React + Redux? 组件props.dispatch不起作用。 反应redux - Component props.dispatch doesnt work. React redux React Redux:从容器到组件传递道具和分派的问题 - React Redux : Problem with passing props and dispatch from container to component 如何在 React 中的其他组件的 MAP 中插入组件? - How do I insert a component within a MAP of other component in React? 如何使用React Router v4将Redux props传递到不同路径中的单独组件? - How do I pass Redux props to a separate component in a different Route with React Router v4? 反应+减少。 我可以调度一个动作来获取一个组件并让另一个组件通过this.props.children呈现它吗? - react + redux. can i dispatch an action that will take a component and have another component render it via this.props.children?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM