尽可能清晰地创建尽可能多的独特美国的最佳方法?

发布于 2025-01-17 18:09:57 字数 3036 浏览 2 评论 0原文

我试图弄清楚如何尽可能干净地创建尽可能多的 useState (牢记 DRY 原则)。

我试图解决的问题是,目前我有一个菜单项列表及其相对价格。该网站允许客户使用下拉菜单来选择他/她想要的该商品的订单数量。

我的问题是,我知道对于一个菜单项,我可以使用 useState 来制作类似键值对的内容

const [order, setOrder] = useState({});

,其中键值对将是 namenumber of order< /code>

现在我的问题是:假设我有 N 个菜单项,如何根据需要创建尽可能多的 const [order, setOrder] = useState({}) ?我知道我不能将 useState 放入 for 循环中,所以这是不可能的(?)

下面是我正在尝试解决的代码:

export default function MenuPage() {

    // getting the menu items and prices from firebase

    const [menuItems, setMenuItems] = useState({});
    React.useEffect(async () => {
        const result = await querySnapShot();
        result.forEach((doc) => {
            setMenuItems(doc.data());
        })
    }, []);

    return(
       
       {
            Object.keys(menuItems).map((key) => {
                return (
                    <Box
                        key={key}
                        display='flex'
                        flexDirection='row'
                        alignItems='center'
                        justifyContent='center'
                        textAlign='center'
                        borderBottom='0.5px solid black'
                        sx={{ my: 10, mx: 10 }}>
                        <Box flex={1} textAlign='center' sx={{ fontSize: '35px', }}>
                            {key}:
                        </Box>
                        <br />
                        <Box flex={1} textAlign='center' sx={{ fontSize: '25px' }}>
                            ${menuItems[key]}0
                        </Box>
                        <Box flex={1} display='flex' flexDirection='row' alignItems='center' justifyContent='end' sx={{ width: '50px' }}>
                            {/* <Typography flex={0} sx={{ mr: 5, fontFamily: 'Poppins', fontSize: '25px' }}>Order: </Typography> */}
                            <Box flex={0.5} textAlign='center'>
                                <FormControl fullWidth>
                                    <InputLabel id="demo-simple-select-label">Order</InputLabel>
                                    <Select
                                        value={order}
                                        name={key}
                                        label="Order"
                                        onChange={handleChange}>
                                        <MenuItem value={1}>1</MenuItem>
                                        <MenuItem value={2}>2</MenuItem>
                                        <MenuItem value={3}>3</MenuItem>
                                    </Select>
                                </FormControl>
                            </Box>
                        </Box>

                    </Box>
                );
            })
        }

    );

}

I am trying to figure out how to create as many useState as cleanly (keeping DRY principle in mind) as possible.

The problem I am trying to solve is that currently I have a menu list of items and its relative prices. The website lets the customer use a drop down menu to choose how many orders of that item he/she wants.

My question is that I know for one menu item, I can use useState to make something like

const [order, setOrder] = useState({});

where the key-value pair would be name and number of orders

Now my question is: suppose I have N many menu items, how can I create as many const [order, setOrder] = useState({}) as I need? I know I can't put useState in a for-loop so that's out of the question (?)

The below is my code I'm trying to work out:

export default function MenuPage() {

    // getting the menu items and prices from firebase

    const [menuItems, setMenuItems] = useState({});
    React.useEffect(async () => {
        const result = await querySnapShot();
        result.forEach((doc) => {
            setMenuItems(doc.data());
        })
    }, []);

    return(
       
       {
            Object.keys(menuItems).map((key) => {
                return (
                    <Box
                        key={key}
                        display='flex'
                        flexDirection='row'
                        alignItems='center'
                        justifyContent='center'
                        textAlign='center'
                        borderBottom='0.5px solid black'
                        sx={{ my: 10, mx: 10 }}>
                        <Box flex={1} textAlign='center' sx={{ fontSize: '35px', }}>
                            {key}:
                        </Box>
                        <br />
                        <Box flex={1} textAlign='center' sx={{ fontSize: '25px' }}>
                            ${menuItems[key]}0
                        </Box>
                        <Box flex={1} display='flex' flexDirection='row' alignItems='center' justifyContent='end' sx={{ width: '50px' }}>
                            {/* <Typography flex={0} sx={{ mr: 5, fontFamily: 'Poppins', fontSize: '25px' }}>Order: </Typography> */}
                            <Box flex={0.5} textAlign='center'>
                                <FormControl fullWidth>
                                    <InputLabel id="demo-simple-select-label">Order</InputLabel>
                                    <Select
                                        value={order}
                                        name={key}
                                        label="Order"
                                        onChange={handleChange}>
                                        <MenuItem value={1}>1</MenuItem>
                                        <MenuItem value={2}>2</MenuItem>
                                        <MenuItem value={3}>3</MenuItem>
                                    </Select>
                                </FormControl>
                            </Box>
                        </Box>

                    </Box>
                );
            })
        }

    );

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離殇 2025-01-24 18:09:57

假设您有一个数组作为响应,您可以:

const data = [
        { name: 'Foo', value: 'bar' },
        { name: 'Foo2', value: 'bar2' },
      ];

      setState(data.map({ name: 'Foo', value: 'bar' }));

      // or if no change has to be done you can set it directly into the state
      setState(data);

Supposing that you have an array as response you can:

const data = [
        { name: 'Foo', value: 'bar' },
        { name: 'Foo2', value: 'bar2' },
      ];

      setState(data.map({ name: 'Foo', value: 'bar' }));

      // or if no change has to be done you can set it directly into the state
      setState(data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文