如何使用React和Redux将分派映射到递归嵌套组件中的props?

问题描述 投票:0回答:1

我正在尝试创建一个显示使用Redux来显示React族谱的应用程序。每个人都显示在一个框内,并带有通向其父母的一行。我想要一个功能,如果您单击某人的“卡片”,则会看到一个弹出窗口,其中显示了更多的传记信息。我遇到的麻烦是要在树中比根级别人员更远的任何组件中映射调度动作。当我单击顶部的“ John”时,它可以按预期工作,但是他的任何祖先都破裂了。希望我只是缺少明显的东西!

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9WSnZWYi5wbmcifQ==” alt =“这里是没有生物卡的家谱”>

“这是我收到的错误消息”

以下是组件:

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;
我正在尝试创建一个显示使用Redux来显示React族谱的应用程序。每个人都显示在一个框内,并带有通向其父母的一行。我想拥有一个功能,如果您...
javascript reactjs redux react-redux
1个回答
2
投票
实际上,您不了解ReactJS的基础,您从未将selectPerson函数传递给Person组件,请注意代码的这一部分:
热门问题
推荐问题
最新问题