Reputation: 3
I am attempting to make an application that displays a family tree to React using 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;
Upvotes: 0
Views: 300
Reputation: 32482
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:
<Person key={rootPerson.id} info={rootPerson}/>
You should pass the selectPerson
to this line. otherwise, you got it as undefined
inside the Person
component
<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
. this is cause of your issue.
Upvotes: 1