简体   繁体   English

React Hooks Modal 通过 state?

[英]React Hooks Modal passing state?

So im working on a React app, and im a bit rusty as I took some time learning other things.所以我正在开发一个 React 应用程序,当我花了一些时间学习其他东西时,我有点生疏了。 It will eventually be connecting to an API but for now im just working on the front-end.它最终将连接到 API 但现在我只是在前端工作。

Anyways I have a modal that uses react-bootstrap.无论如何,我有一个使用 react-bootstrap 的模式。 Here is the modal (it's a simple "update" form for right now).这是模式(现在是一个简单的“更新”表单)。

import React from "react";
import { useState } from "react";
import axios from "axios";
import Modal from "react-bootstrap/Modal";
import Form from "react-bootstrap/Form";
import Button from "react-bootstrap/Button";

const SensorModalForm = props => {
  const [name, setName] = useState(props.name);
  const [device, setDevice] = useState(props.device);

  return (
    <Modal show={props.show} onHide={props.handleClose}>
      <Modal.Header closeButton>
        <Modal.Title>Edit Form</Modal.Title>
      </Modal.Header>
      <Modal.Body>
        <Form onSubmit={props.handleSubmit}>
          <Form.Group controlId="formBasicName">
            <Form.Label>Device Name</Form.Label>
            <Form.Control
              type="text"
              value={name}
              onChange={e => setName(e.target.value)}
            />
            <Form.Text className="text-muted">
              We'll never share your email with anyone else.
            </Form.Text>
          </Form.Group>
          <Form.Group controlId="formBasicDevice">
            <Form.Label>Device Type</Form.Label>
            <Form.Control
              type="text"
              value={device}
              onChange={e => setDevice(e.target.value)}
            />
          </Form.Group>
          <Button variant="primary" onClick={props.handleSave}>
            Save Changes
          </Button>
          <Button variant="secondary" onClick={props.handleClose}>
            Close
          </Button>
        </Form>
      </Modal.Body>
      <Modal.Footer>text</Modal.Footer>
    </Modal>
  );
};

export default SensorModalForm;

It's pretty basic, and it's also my first time using react hooks (I literally just sorta adapted an example to this).它非常基础,也是我第一次使用 React Hooks(我实际上只是为此改编了一个例子)。

The "card" component that calls this is here (Which is part of a table component which is what has the state with all the sensors currently).调用它的“卡”组件在这里(它是表组件的一部分,该组件具有 state 和当前所有传感器)。 Here is the card component:这是卡片组件:

/* eslint-disable */
import React, { useState } from "react";
import SensorModalForm from './SensorModalForm'
import { Link } from "react-router-dom";
import Button from "react-bootstrap/Button";


const SensorCard = ({ props, sensor, toggleSensor }) => {
  const [show, setShow] = useState(false);

  const handleSave = () => {
    setShow(false)
    console.log("Submitted")
  }
  const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);

  if (sensor) {
    const { id, name, device, temp, humidity, active } = sensor;
    return (
      <tr>
        <td>{id}</td>
        <td>{name}</td>
        <td>{device}</td>
        <td>{temp}</td>
        <td>{humidity}</td>
        <td>{active ? "True" : "False"}</td>
        <td>
          <Link
            to={{
              pathname: `/devices/${id}`,
              state: { sensor: sensor }
            }}
            className="btn btn-info btn-sm"
          >
            View
          </Link>
          <SensorModalForm device={device} name={name} handleClose={handleClose} handleSave={handleSave} show={show}/>
          <Button variant="info" size="sm" onClick={handleShow}>
            Edit
          </Button>
          <Button variant="info" size="sm" data-id={id} onClick={toggleSensor}>
            Toggle Active
          </Button>
        </td>
      </tr>
    );
  }
};

export default SensorCard;

The problem Im running into is I want to do an axios PUT/PATCH call with the new updated values once I Submit/Save the modal (react-bootstrap is where I got the setShow hooks etc... from).我遇到的问题是我想在提交/保存模式后使用新的更新值进行 axios PUT/PATCH调用(react-bootstrap 是我从中获得setShow挂钩等的地方)。 However I am not exactly sure how I can pass those values down?但是我不确定如何将这些值传递下来?

I feel like this is one of those instances where i've "nested" too far down and using redux would be better...but it's a simple app and really I don't want to force redux if I can solve this small problem.我觉得这是我“嵌套”得太深的情况之一,使用 redux 会更好......但它是一个简单的应用程序,如果我能解决这个小问题,我真的不想强迫 redux . I am pretty rusty at this so I think there is something obvious I am missing, but essentially I want to call an axios PUT request on the handleSave callback within SensorCard .我对此很生疏,所以我认为我缺少一些明显的东西,但基本上我想在SensorCard中的handleSave回调上调用 axios PUT请求。

Is this possible?这可能吗? I feel like I could just call e.target.name.value or is that bad practice?我觉得我可以打电话给e.target.name.value还是那是不好的做法?

Within you Modal code, you must pass the values that you want to save by calling your save method like this:在您的模态代码中,您必须通过调用保存方法来传递要保存的值,如下所示:

<Button variant="primary" onClick={() => props.handleSave(name, device)}>

And in your SensorCard, your function signature should expect those parameters在您的 SensorCard 中,您的 function 签名应该期望这些参数

const handleSave = (name, device) => {
  // Your implementation
}

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

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