如何在 React 中使用特定 ID 进行 Axios Post 请求?

问题描述

我有一个图像滑块。对于每张图片用户可以点击“喜欢”来支持 (+1) 或“不喜欢”来反对 (-1)。我不知道如何使用axios来操作json文件。我正在使用 json-server 来模拟后端。

但似乎我只能运行 json-server 并打印出 results.json 或运行 localhost:3000 来查看我的 React 应用程序。此外,我无法为用户正在查看的每张图片(以及点击喜欢或不喜欢的图片)发出单独的 POST 请求。

这是代码

  // save axios get request in designs state

  const [designs,setDesigns] = useState([]);
  
  // get designs from results.json

  useEffect(() => {
    axios.get("json/results.json").then((response) => {
      setDesigns(response.data.results);
    });
  },[]);


  // Now i want to send a "-1" to the results.json for that specific image
  // when a user clicks dislike

  const nextSlideanddislike = (id,Vote) => {

    axios
      .post("http://localhost:3000/results/{id}",{
        Vote: Vote - 1,})

    // send downVote to results.json for this id
  };

在实际渲染中,我正在迭代设计:

      {designs.map((design,index) => {
        return (
          ...
            <FontAwesomeIcon
              className={styles.leftArrow}
              onClick={() => nextSlideanddislike(design.id,design.Vote)}
              icon={faArrowCircleLeft}
            />    
        
            <p>disLIKE IMAGE</p>

            <img
              className={styles.image}
              src={design.url}
              id={design.id}
              Vote={design.Vote}
              alt='design'
            />
            ...
        );
      })}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...