简体   繁体   English

将条带js集成到react withnode中

[英]integrate stripe js in react withnode

I want to set up a stripe checkout using react and node.js.我想使用 react 和 node.js 设置条带结帐。 Compiles in node terminal but when I inspect the localhost:3000 page, this error message is returned在节点终端中编译,但是当我检查 localhost:3000 页面时,返回此错误消息

Each child in a list should have a unique "key" prop.列表中的每个孩子都应该有一个唯一的“关键”道具。

Check the render method of Cart .检查Cart的渲染方法。

  1. in tr (at cart/index.jsx:66)在 tr 中(在 cart/index.jsx:66)
  2. in Cart (at src/index.jsx:8)在购物车中(在 src/index.jsx:8)
  <tbody>{
                  items.map(item => (
                   <tr>
                   <td>{item.name}</td>
                   <td>
                    <img
                      src={`/images/${item.apiId}.jpg`}
                      alt={item.name}
                      width={180}
                   />
                    </td>
                    <td>{item.quantity}</td>
                    <td>{formatPrice(item.price)}</td>
                </tr>)
                 )}
               <tr>
import React from 'react'
import ReactDOM from 'react-dom'

import Cart from "./components/cart/"


ReactDOM.render(
    <Cart stripeToken = "test-token" />,
    document.getElementById("root")
)   

Anytime you render a list of elements, you need to add a key prop to each element so React can keep track of which elements to re-render if the component's state or props ever change.任何时候渲染元素列表时,都需要为每个元素添加一个key prop,以便 React 可以跟踪在组件的 state 或 props 发生变化时要重新渲染的元素。 In your case, to fix that warning message you would add the key prop with a value that is unique to each table row.在您的情况下,要修复该警告消息,您将添加key prop,其值对每个表行都是唯一的。 For example:例如:

        <tbody>
          {items.map((item) => (
            <tr key={item.apiId}> // Makes the warning go away
              <td>{item.name}</td>
              <td>
                <img
                  src={`/images/${item.apiId}.jpg`}
                  alt={item.name}
                  width={180}
                />
              </td>
              <td>{item.quantity}</td>
              <td>{formatPrice(item.price)}</td>
            </tr>
          ))}
        </tbody>

In this case, I'm using the item.apiId as the key under the assumption that the apiId 's are unique to each element in the items array.在这种情况下,我使用item.apiId作为键,假设apiId对于 items 数组中的每个元素都是唯一的。 It is somewhat common to see the item's index in the array being used as a unique key, but that approach falls apart quickly if the order of the elements ever changes.将数组中的项目索引用作唯一键有点常见,但如果元素的顺序发生变化,这种方法很快就会崩溃。

You can read more on the key prop and why it's needed here:您可以在此处阅读有关 key 道具的更多信息以及为什么需要它:

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

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