我正试图弄清楚如何传递一些组件作为 prop ,然后使用它们。我有这个工作在一个文件中,但想把它分解成一个可重用的组件。
这是一个页面我有:career.tsx
export function Full() {
return ( <Text>Full</Text> );
}
export function Condensed() {
return ( <Text>Condensed</Text> );
}
export function Video() {
return ( <Text>Video</Text> );
}
export default function Career() {
const [activeComponent, setActiveComponent] = useState(null);
return (
<>
<Title>Career</Title>
{/* Passing components as props */ }
<ModuleToggle_withState video={<Video/>} condensed={<Condensed/>} full={<Full/>}
setActiveComponent={setActiveComponent} />
{ activeComponent }
</>
);
}
模块切换状态.tsx
export function ModuleToggle_withState({Video, Condensed, Full}:any, setActiveComponent:any) {
// Will research typescript types and update the "any"
// Trying to figure out how to pass and use Components which were passed
useEffect(() => {
switch(section) {
case 'video':
setActiveComponent=<Video/>
case 'condensed':
setActiveComponent=<Condensed/>
case 'full':
setActiveComponent=<Full/>
}
}, [section]);
return (
...
);
}
2条答案
按热度按时间9avjhtql1#
最好有条件地呈现内容。
使用它:
laik7k3q2#
你必须创建一个布局:
另一个组成部分: