reactjs - React - 子组件的自定义布局
问题描述
我正在编写一个小部件库,我想让用户灵活地使用他们自己的布局。例如,我有一个Tracker
小部件负责跟踪一个点的当前位置。这里是使用的概述Tracker
<Tracker initialPos={[0,0]}> //<-- holds state of current position
<TrackerPlot option={1} />
<TrackerPlot option={2} />
<TrackerPlot option={3} />
...
<TrackerPlot option={n} />
<TrackerControls />
</Tracker>
这很好,但我想为库的用户提供额外的灵活性来添加自定义布局,以便他们可以将 Tracker 组件放置在他们喜欢的位置。就像是:
<Tracker initialPos={[0,0]}>
<div class="USER-DEFINED-LAYOUT-PLOTS">
<TrackerPlot val={1} />
<TrackerPlot val={2} />
<TrackerPlot val={3} />
...
<TrackerPlot val={n} />
</div>
<div class="USER-DEFINED-LAYOUT-CONTROLS">
<TrackerControl />
</div>
</Tracker>
问题是我一直在使用儿童道具如下:
function Tracker(props){
const [pos, setPos] = useState(props.initialPosition);
const children = React.Children.map(props.children, child=>{
return React.cloneElement(child, {
pos:pos
});
});
return {children};
}
如果我可以灵活地添加周围环境<div>
,那么上面访问子项的代码就会中断。是否有模式/工具/设计允许用户使用子组件定义自己的布局
解决方案
这是context api的经典用法,您的父母将成为提供者,您的孩子将成为消费者。
// PosContext.jsx
export const PosContext = React.createContext({
pos: initialPosition
});
//Tracker.jsx
import {PosContext} from './PosContext';
export const Tracker = ({ initialPos, children }) => (
<PosContext.Provider value={{ pos: initialPos }}>{children}</PosContext.Provider>
);
// TrackerPlot.jsx
import { PosContext } from './PosContext';
export const TrackerPlot = props => (
<PosContext.Consumer>{({ pos }) => <div>{pos}</div>}</PosContext.Consumer>
);
推荐阅读
- java - 当我尝试在 C# 上启动这个 .jar 文件时,为什么它不会运行?
- git - 通过cherry-pick将行添加到不同的位置导致冲突
- python - NOT NULL 约束失败:auth_user.password
- react-native - 将导航从子组件传递到父组件获取 TypeError
- flutter - Flutter:如何从 Firebase 获取实时数据库(导入的 Json)中的数据
- python-3.x - 通过 stackoverflow 错误使用 Selenium 登录 Google
- java - Jitpack 任务“安装”在根项目 Android 库中不明确
- java - wsdl 中缺少元素
- elixir - 如何在 LiveView 中更新嵌套分配的值
- amazon-web-services - AWS ALB 的 JWT 声明验证返回 403 Access Denied