这种模式经常用于由侧边栏、主栏等组成的常见布局。
・app.js
import { splitscreen } from "./components/split-screen"; const leftside = ({ title }) => { return <h2 style={{ backgroundcolor: "red" }}>{title}</h2>; }; const rightside = ({ title }) => { return <h2 style={{ backgroundcolor: "blue" }}>{title}</h2>; }; function app() { return ( <splitscreen leftwidth={1} rightwidth={3}> <leftside title={"left"} /> <rightside title={"right"} /> </splitscreen> ); } export default app;
・该组件将 splitscreen 组件中的 leftside 和 rightside 组件作为子组件包装。
・我将标题道具传递给 leftside 和 rightside 组件。
·我将 leftwidth 和 rightwidth 属性传递给 splitscreen 组件,以便我可以更改每个组件的宽度。
・split-screen.js
import React from "react"; import { styled } from "styled-components"; const Container = styled.div` display: flex; `; const Panel = styled.div` flex: ${(p) => p.flex}; `; export const SplitScreen = ({ children, leftWidth = 1, rightWidth = 1 }) => { const [left, right] = children; return ( <Container> <Panel flex={leftWidth}>{left}</Panel> <Panel flex={rightWidth}>{right}</Panel> </Container> ); };
・该组件由左组件和右组件组成,它们作为子组件接收。
・我可以将接收 props 的每个组件的宽度更改为 leftwidth 和 rightwidth。
以上就是React 设计模式~布局组件~的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号