I am integrating React into an existing app. This app is data intensive and the data structure is pretty complicated which makes it a bit hard for me to adapt the React patterns, especially statelessness and composition.
Given data like this:
component: {
options: [optionA, optionB, optionC]
}
options: {
optionA : {
name: 'FOO',
choices: [choiceA, choiceB, choiceC]
},
optionB : {
name: 'BAR',
choices: [choiceD, choiceE]
},
...
}
choices: {
choiceA : {
type: 'typeA',
choices: [choiceA, choiceB, choiceC],
name: 'abb'
},
choiceB : {
type: 'typeB',
name: 'abc'
},
...
}
Since this data is linked by ids I have two possibilities:
Retrieve children data in parent component and pass it to the children.
Pass the ID and children retrieves it's own data.
One implies dynamically retrieving a components props and the other implies having a "god" parent which has all the necessary data for it's children, which one is the better approach?
My other question is if the component that takes a Choice as its props should display differently depending on the type of it's Choice, is the better approach to make a wrapper component like this? :
class Choice extends Component {
constructor(props){
super(props);
}
render(){
switch(props.choice.type){
case "typeA":
return (<TypeAComponent />);
case "typeB":
return (<TypeBComponent />);
default:
return (..);
}
}
}
Or is there a cleaner alternative (I'm a bit allergic to switch cases)...