基于使用唯一 ID 搜索的渲染值 - ReactJS

Render Values Based on Searching with Unique ID - ReactJS

我使用 reactjs、nodejs 和 mysql.. 从数据库中获取数据。 现在我的挣扎是我需要仅根据搜索值显示行,可能不包括提交按钮(如 google 搜索)

这是我的 NodeJS 获取 link,所以如果我输入 http:\localhost\productlist,那么它将仅在 NodeJS 中显示第三个产品,但我也想在 reactjs 中实现相同的功能通过搜索框

app.get("/productslist/:idaddproducts", (req, res) => {
  const id = req.params.idaddproducts
  console.log(id)
  connection.query("SELECT * FROM `addproducts` where idaddproducts = ? ",id,(err, rows) => {
    if (!err) console.log(rows), res.json({ data: rows });
    //data variable is not mandatory but inorder to pass the result we need it
    else console.log(err);
    // connection.end()
  });
});

这是我的反应 js 代码

export default function MaterialTableDemo() {
  const initialState = {
    idaddproducts: "",
    productName: "",
    productId: "",
    productBrand: "",
    productQuantity: "",
    productPrice: "",
    productType: ""
  };
  const [poName, pnName] = React.useState(initialState);

  const handleName = e => {
    e.preventDefault();
    pnName({ ...poName, [e.target.name]: e.target.value });
    console.log(poName);
  };

  async function handleTable(e) {
    const res = await fetch("http://localhost:4000/productslist/:id");
    const data = await res.json();
    pnName(data.data);
    console.log(data.data);
  }

  useEffect(() => {
    handleTable();
  }, []);

  const classes = useStyles();
  return (
    <form className={classes.root} noValidate autoComplete="off" align="center">
      <div className={classes.formGroup}>
        <FormControl>
          <Input
            type="search"
            label="Product ID"
            variant="outlined"
            size="small"
            placeholder="Enter Product ID"
            value={poName.idaddproducts}
            name="idaddproducts"
            onChange={handleName}
          />
        </FormControl>
        {poName.length &&
          poName.map(v => {
            return (
              <h4 key={v.idaddproducts}>
                {v.productName}
                {v.productId}
                {v.productBrand}
                {v.productQuantity}
                {v.productPrice}
                {v.productType}
              </h4>
            );
          })}
      </div>
    </form>
  );
}

任何帮助将不胜感激

async function handleTable(e) {
    const id = poName.idaddproducts;
    const res = await fetch("http://localhost:4000/productslist/" + id);
    const data = await res.json();
    pnName(data.data);
    console.log(data.data);
}

useEffect(() => {
    handleTable();
}, [poName.idaddproducts]);

试试这样用

检查这个,这将工作正常,如果不是那么会有另一个问题

它现在可以工作了...这是一段代码..感谢那两个人

export default function MaterialTableDemo() {
  const initialState = {
    idaddproducts: "",
    productName: "",
    productId: "",
    productBrand: "",
    productQuantity: "",
    productPrice: "",
    productType: ""
  };
  const [poName, pnName] = React.useState(initialState);

  const handleName = e => {
    const idAddProducts = e.target.value;
    e.preventDefault();
    pnName({ ...poName, idaddproducts: idAddProducts });
    handleTable(idAddProducts);
    console.log(poName);
  };

  async function handleTable(idAddProducts) {
    const id = poName.idaddproducts;
    const res = await fetch(
      "http://localhost:4000/productslist/" + idAddProducts
    );
    const data = await res.json();
    pnName(data.data);
    console.log(data.data);
  }

  useEffect(() => {
    handleTable();
  }, []);

  const classes = useStyles();
  return (
    <form className={classes.root} noValidate autoComplete="off" align="center">
      <div className={classes.formGroup}>
        <FormControl>
          <Input
            type="search"
            label="Product ID"
            variant="outlined"
            size="small"
            placeholder="Enter Product ID"
            value={poName.idaddproducts}
            name="idaddproducts"
            onChange={handleName}
          />
          {poName.length &&
            poName.map(v => {
              return (
                <h4 key={v.idaddproducts}>
                  {v.productName}
                  {v.productId}
                  {v.productBrand}
                  {v.productQuantity}
                  {v.productPrice}
                  {v.productType}
                </h4>
              );
            })}
        </FormControl>
      </div>
    </form>
  );
}

See ScreenShot