简体   繁体   English

不断收到TypeError:当我单击复选框时无法读取未定义的属性“地图”,不知道问题出在哪里

[英]Keep getting TypeError: Cannot read property 'map' of undefined when I click on checkbox, dont know where the problem is

So I keep getting this error when I click on checkbox and the desired outcome would be once the checkbox is clicked the property of active should change to opposite.因此,当我单击复选框时,我不断收到此错误,并且期望的结果是单击复选框后,active 的属性应更改为相反。 Even If I remove the activeHandler function once i click on checkbox I get the same error but now for the initial mapping of products in tbody即使我在单击复选框后删除了 activeHandler 函数,我也会收到相同的错误,但现在对于 tbody 中产品的初始映射

const ProductList = props => {
const [products, setProducts] = useState(
    [
        {
            id: 1,
            name: 'Product 1',
            ean: 242355,
            type: 'Food',
            weight: 24,
            color: 'blue',
            active: true,
            quantity: 2,
            price: 25
        },
        {
            id: 2,
            name: 'Product 2',
            ean: 57434,
            type: 'Food',
            weight: 48,
            color: 'red',
            active: false,
            quantity: 5,
            price: 12
        }
    ]
);

const activeHandler = productId => {
    setProducts(prevState => {
        const updatedProducts = prevState.products.map(prod => {
            if (prod.id === productId) {
                prod.active = !prod.active
            }
            return prod
        })
        return {
            products: updatedProducts
        }
    })
}

return (
    <div>
        <table className="table">
            <thead>
                <tr>
                <th scope="col">Name</th>
                <th scope="col">EAN</th>
                <th scope="col">Type</th>
                <th scope="col">Weight</th>
                <th scope="col">Color</th>
                <th scope="col">Active</th>
                <th></th>
                </tr>
            </thead>
            <tbody>
            {products.map(product => (
                <tr key={product.id}>
                <td>{product.name}</td>
                <td>{product.ean}</td>
                <td>{product.type}</td>
                <td>{product.weight}</td>
                <td>{product.color}</td>
                <td>
                    <input type="checkbox" checked={product.active} onChange={() => activeHandler(product.id)} />
                </td>
                <td>
                <button className="btn btn-secondary mr-1" disabled={product.active}>VIEW</button>
                <button className="btn btn-primary mr-1" disabled={product.active}>EDIT</button>
                <button className="btn btn-danger" disabled={product.active}>DELETE</button>
                </td>
                </tr>
            ))     
            }
            </tbody>
       </table>
    </div>
)

} }

Your prevState is the actual array in this case, so you should map over it and also return it as the new state and not an object with a products key:在这种情况下,您的prevState是实际数组,因此您应该映射它并将其作为新状态而不是具有products键的对象返回:

setProducts(prevState => {
        const updatedProducts = prevState.map(prod => {
            if (prod.id === productId) {
                prod.active = !prod.active
            }
            return prod
        })
        return updatedProducts
    })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不断收到此类型错误:无法读取未定义的属性“地图” - I keep getting this TypeError: Cannot read property 'map' of undefined 我不断收到一条错误消息“未捕获的类型错误:无法读取未定义的属性‘点击’”有人知道怎么了吗? - I keep getting an error saying “Uncaught TypeError: Cannot read property 'click' of undefined” does anyone know what's wrong? 我不断收到类型错误:无法读取未定义的属性“缓存” - I keep getting the TypeError: Cannot read property 'cache' of undefined ×获取TypeError:无法读取未定义的属性“ map” - × Getting a TypeError: Cannot read property 'map' of undefined 得到这个类型错误:无法读取未定义的属性“地图” - getting this TypeError: Cannot read property 'map' of undefined 不断收到TypeError:无法读取未定义的属性&#39;formatted_address&#39; - Keep getting TypeError: Cannot read property 'formatted_address' of undefined 我试图 map 数据库中的数据并收到此错误。 TypeError:无法读取未定义的属性“地图” - I trying to map the data from the database and getting this error. TypeError: Cannot read property 'map' of undefined TypeError:无法读取未定义的属性“公会”我正在编写欢迎代码,但我不断收到该错误 - TypeError: Cannot read property 'guild' of undefined I'm writing a welcome code but I keep getting that error 获取TypeError:无法读取未定义错误的属性“map” - Getting TypeError: Cannot read property 'map' of undefined error 我在谷歌表上一直收到这个错误“类型错误:无法读取未定义的 searchRecord @ Code.gs:213 的属性 &#39;0&#39;” - I KEEP GETTING THIS ERROR on google sheet “TypeError: Cannot read property '0' of undefined searchRecord @ Code.gs:213”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM