如何在 React 中将多个值添加到动态 table?

How do I add multiple values to a dynamic table in react?

我有一个 table 从两个选择中收集用户输入。当用户选择一个字段时,我已经成功地做到了,因此他们的输入显示在 table 行中,并且任何新输入都会触发新行。

但是我很难让它显示多个值而不是一个值。这是它目前的样子:

它说输入示例'P2P2'的地方应该只说'P2';每次添加新行时,'P2' 都会成功添加,但它总是相互叠加,不像这样的 'Amazon' 输入:

我知道我做错了什么并尝试了多种方法,但我实际上不知道问题是什么或如何解决问题。这是代码:

 
  const [shop, setShop] = useState([]);
  
  const [profil, setProfil] = useState([]);

  const [formShop, setFormShop] = useState(undefined);
  
  const [formProfil, setFormProfil] = useState(undefined);
  
  function addShop(s){
    setShop((currentShops) => [...currentShops, s]);
  }

  
  function addProfile(p){
    setProfil((currentProfiles) => [...currentProfiles, p]);
  }

  function handleSubmit(e){
    e.preventDefault();

    addShop(formShop);
    addProfile(formProfil);

  }

        ......

                    <tr>
                      {(shop.map((s) => (
                        <tr>
                        <td>{s}</td>
                        <td>{profil}</td>
                        </tr>
                      )))}
                    </tr>
                    
                    ........
                    //snippets of form select

                    <Input type="select" name="selectStore" id="selectStore" onChange={(e) => setFormShop(e.target.value)}>

                    ........

                    <Input type="select" name="selectProf" id="selectProf" onChange={(e) => setFormProfil(e.target.value)}>

                    ........


我建议您使用一个对象来管理配对商店-配置文件,这是一个有效的示例。

const {useState} = React;
const {
  Modal,
  ModalHeader,
  ModalBody,
  ModalFooter,
  Input,
  FormGroup,
  FormText,
  Form,
  Label,
  Button,
  Card,
  CardHeader,
  CardBody,
  CardTitle,
  //CardFooter,
  Table,
  Row,
  ButtonGroup,
  Col,
} = Reactstrap;

const App = (props) => {
  const [shopProfil, setShopProfil] = useState([]);


  const [formShop, setFormShop] = useState(undefined);

  const [formProfil, setFormProfil] = useState(undefined);
console.log(formShop,formProfil)


  function addShopProfil(obj) {
    setShopProfil((currentShopProfiles) => {
      const curr = currentShopProfiles.slice()
      curr.push(obj);
      return curr
    })
  }

  function handleSubmit(e) {
    e.preventDefault();
    const objshopProfil ={
      formshop:formShop,
      formprofil:formProfil
    };

    
    addShopProfil(objshopProfil);
    
  }

  return (
    <div>
      <div>
        <Table>
          <tbody>
            <tr>
              {shopProfil.map((s,index) => (
                <tr key={`${s.formshop+s.formprofil+index}`}>
                  <td>{s.formshop}</td>
                  <td>{s.formprofil}</td>
                </tr>
              ))}
            </tr>
          </tbody>
        </Table>
      </div>

      <Form onSubmit={handleSubmit}>
        <FormGroup>
          <Label for="selectStore">Store</Label>
          <Input
            type="select"
            name="selectStore"
            id="selectStore"
            onChange={(e) => setFormShop(e.target.value)}
          >
            <option>-</option>
            <option>Amazon </option>
            <option>Local</option>
            <option>Target</option>
          </Input>
        </FormGroup>
        <FormGroup>
          <Label for="selectProf">Profile</Label>
          <Input
            type="select"
            name="selectProf"
            id="selectProf"
            onChange={(e) => setFormProfil(e.target.value)}
          >
            <option>-</option>
            <option>P1</option>
            <option>P2</option>
            <option>P3</option>
            <option>P4</option>
          </Input>
        </FormGroup>
        <button type="submit">button</button>
      </Form>
    </div>
  );
};

ReactDOM.render(
  <App />,
  document.getElementById('app')
);
<script src="https://npmcdn.com/react@15/dist/react-with-addons.js"></script>
<script src="https://npmcdn.com/reactstrap@3/dist/reactstrap.min.js"></script>
<link href="https://npmcdn.com/bootstrap@4.0.0-alpha.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>


<div id="app"></div>

或者,如果您想保持相同的结构,请看这里:

const {useState} = React;
const {
  Modal,
  ModalHeader,
  ModalBody,
  ModalFooter,
  Input,
  FormGroup,
  FormText,
  Form,
  Label,
  Button,
  Card,
  CardHeader,
  CardBody,
  CardTitle,
  //CardFooter,
  Table,
  Row,
  ButtonGroup,
  Col,
} = Reactstrap;

const App = (props) => {
  const [shop, setShop] = useState([]);

  const [profil, setProfil] = useState([]);

  const [formShop, setFormShop] = useState(undefined);

  const [formProfil, setFormProfil] = useState(undefined);

  function addShop(s) {
    setShop((currentShops) => [...currentShops, s]);
  }

  function addProfile(p) {
    setProfil((currentProfiles) => [...currentProfiles, p]);
  }

  function handleSubmit(e) {
    e.preventDefault();

    addShop(formShop);
    addProfile(formProfil);
  }

  return (
    <div>
      <div>
        <Table>
          <tbody>
            <tr>
              {shop.map((s,i) => (
                <tr key={`${s+i}`}>
                  <td>{s}</td>
                  <td>{profil[i]}</td>
                </tr>
              ))}
            </tr>
          </tbody>
        </Table>
      </div>

      <Form onSubmit={handleSubmit}>
        <FormGroup>
          <Label for="selectStore">Store</Label>
          <Input
            type="select"
            name="selectStore"
            id="selectStore"
            onChange={(e) => setFormShop(e.target.value)}
          >
            <option>-</option>
            <option>Amazon </option>
            <option>Local</option>
            <option>Target</option>
          </Input>
        </FormGroup>
        <FormGroup>
          <Label for="selectProf">Profile</Label>
          <Input
            type="select"
            name="selectProf"
            id="selectProf"
            onChange={(e) => setFormProfil(e.target.value)}
          >
            <option>-</option>
            <option>P1</option>
            <option>P2</option>
            <option>P3</option>
            <option>P4</option>
          </Input>
        </FormGroup>
        <button type="submit">button</button>
      </Form>
    </div>
  );
};

ReactDOM.render(
  <App />,
  document.getElementById('app')
);
<script src="https://npmcdn.com/react@15/dist/react-with-addons.js"></script>
<script src="https://npmcdn.com/reactstrap@3/dist/reactstrap.min.js"></script>
<link href="https://npmcdn.com/bootstrap@4.0.0-alpha.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>


<div id="app"></div>