简体   繁体   中英

How to add products dynamically in redux initial state?

I am facing complexity on how to add dynamic products in the redux initial state in the next.js redux part?

Currently in my initial state something like that:

const initState = {
    products: [
        {
            id: 1,
            title: "Long Sleeve Leopard T-Shirt",
            price: 250,
            image: require("../../images/products/img1.jpg")
        },
        {
            id: 2,
            title: "Gildan Men's Crew T-Shirt",
            price: 150,
            image: require("../../images/products/img4.jpg")
        }],
    addedItems:[],
    total: 0,
    shipping: 0
}

That is static and now I want load products here dynamically from a server like Firebase.

I was tried creating a function like addToCart() which is currently working for adding products into the cart but didn't luck on that.

You can see my current cartReducer file from the below link.

Here is the Gist

When you load data dynamically from a server, you can use actions and reducers to update the current state. The initial state should just be an empty object or list. You don't change the initial state dynamically. Instead, you update the current state as you load data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM