有一个组件接收道具。
props 有一个数组的形状,对于这个数组的每个元素,一个函数将返回一个不同的组件来渲染。
function MainComponent ({ data }) => { // data is props, an array
const specialFunction = (index) => {
switch (index) {
case 0:
return <Component0 />;
case 1:
return <Component1 />;
case 2:
return <Component2 />;
default:
return null;
}
};
...
return (
...
data.map((item, index) => {
... // do other stuff with item
<div>{specialFunction(index)}</div> // the function that we talk about
...
);
如果道具不会改变,有没有办法记住这个结果?或者有什么更好的写法?