如何实现不仅是一种设置切换而且还有多种设置切换?
我使用 Shopify Polaris 的设置切换。https://polaris.shopify.com/components /actions/setting-toggle#navigation
我不仅想实现一个设置切换,而且还想实现多个设置切换。但我不想总是重复相同的handleToggle() 和值(contentStatus,textStatus)就像沙箱A,B,C下面一样...
import React, { useCallback, useState } from "react";
import { SettingToggle, TextStyle } from "@shopify/polaris";
export default function SettingToggleExample() {
const [activeA, setActiveA] = useState(false);
const [activeB, setActiveB] = useState(false);
const handleToggleA = useCallback(() => setActiveA((active) => !active), []);
const handleToggleB = useCallback(() => setActiveB((active) => !active), []);
const contentStatusA = activeA ? "Deactivate" : "Activate";
const contentStatusB = activeB ? "Deactivate" : "Activate";
const textStatusA = activeA ? "activated" : "deactivated";
const textStatusB = activeB ? "activated" : "deactivated";
const useHandleToggle = (active, setActive) => {
const handleToggle = useCallback(() => setActive((active) => !active), []);
const contentStatus = active ? "Disconnect" : "Connect";
const textStatus = active ? "connected" : "disconnected";
handleToggle();
return [contentStatus, textStatus];
};
useHandleToggle(activeA, setActiveA);
return (
<>
<SettingToggle
action={{
content: contentStatusA,
onAction: handleToggleA
}}
enabled={activeA}
>
This setting is <TextStyle variation="strong">{textStatusA}</TextStyle>.
</SettingToggle>
<SettingToggle
action={{
content: contentStatusB,
onAction: handleToggleB
}}
enabled={activeB}
>
This setting is <TextStyle variation="strong">{textStatusB}</TextStyle>.
</SettingToggle>
</>
);
}
https://codesandbox.io/s/vigorous-pine-k0dpib?file=/App.js
所以我想我可以使用自定义钩子。但这不起作用。因此,如果您给我一些建议,将会很有帮助。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我第一次尝试重构时会在公共处理程序 It 函数上使用参数
,但感觉有点天真。对于更多 React 风格的东西,reducer 可能是最好的选择。
使用减速器
这看起来更干净,并且如果您需要更多切换,则肯定更具可扩展性。
使用包装组件
子组件可以消除“A”和“B”后缀
My first attempt to refactor would use a parameter on the common handler
It functions, but feels a bit naïve. For something more React-ish, a reducer might be the way to go.
With a reducer
This seems cleaner, and is definitely more extensible if you need more toggles.
With a wrapper component
A child component can eliminate the 'A' and 'B' suffixes
为每个切换使用简单的布尔值
如果将活动状态对象组合到单个数组中,则可以动态更新任意数量的设置。下面是一个可能看起来像这样的示例:
当然,您可能希望为其中的每一个添加一个标签,因此最好在函数范围之外定义一个 defaultState 对象并替换
Array( 2).fill(false)
用它。然后,除了可以添加到.map(.. .)
。为每个切换添加标签
根据您的后续操作,这里也是在 CodeSandbox 用于每个切换带有标签的状态(包括此处的答案以防止链接衰减):
Using simple Booleans for each toggle
If you combine your active state objects into a single array, then you can update as many settings as you would like dynamically. Here's an example of what that might look like:
Of course, you will likely want to add a label to each of these going forward, so it may be better to define a defaultState object outside the function scope and replace the
Array(2).fill(false)
with it. Then you can have a stringlabel
property for each toggle in addition to a booleanactive
property which can be added next to each toggle in the.map(...)
.With labels added for each toggle
Per your follow up, here is the implementation also found in the CodeSandbox for a state with labels for each toggle (including here on the answer to protect against link decay):