我正在将React集成到一个现有应用程序中。这个应用程序是数据密集型的,数据结构相当复杂,这使得我有点难以适应React模式,特别是无状态和组合。
给定这样的数据:
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'
},
...
}
在父组件中检索子组件的数据并将其传递给子组件。
传递ID,子组件检索自己的数据。
我的另一个问题是,如果以Choice作为其道具的组件应根据其Choice的类型显示不同,则更好的方法是制作像这样的包装器组件吗?:
class Choice extends Component {
constructor(props){
super(props);
}
render(){
switch(props.choice.type){
case "typeA":
return (<TypeAComponent />);
case "typeB":
return (<TypeBComponent />);
default:
return (..);
}
}
}
还有没有更简洁的替代方法(我对 switch 语句有点过敏)...