简体   繁体   English

我正在尝试通过单击使用 React.js 的按钮来更改图像,但出现错误

[英]I am trying to change image by clicking buttons using React.js but getting error

I want my code to change Image when I click a button but it is giving me error saying "Warning: Expected onClick listener to be a function, instead got a value of object type."我希望我的代码在单击按钮时更改图像,但它给了我错误消息“警告:预期onClick侦听器是 function,而不是object类型的值。” Here is the code:-这是代码: -

import React from 'react'
import './BigAd.css'
import ad1 from './ad1.jpg'
import ad2 from './ad2.jpg'
import ad3 from './ad3.jpg'
import ad4 from './ad4.png'
export default function BigAd() {
    const ChangeImage = async (fileName)=>{
       let img = document.querySelector("#BannerAd")
        img.setAttribute("src", fileName)
    }
  return (
    <div>
         <div className="container">
        <img id='BannerAd' src={ad1} alt="Ad"/>
        <button onClick={ChangeImage({ad1})}>button 2</button>
        <button onClick={ChangeImage({ad2})}>button 3</button>
        <button onClick={ChangeImage({ad3})}>button 1</button>
        <button onClick={ChangeImage({ad4})}>button 4</button>
        </div> 

    </div>
  )
}

So my method was completely wrong this is not how you manipulate DOM in react.js.所以我的方法是完全错误的,这不是你在 react.js 中操作 DOM 的方式。 I had to use UseState to get my code working here is how I fixed it:-我必须使用 UseState 来让我的代码在这里工作,这就是我修复它的方式:-

import React, {useState} from 'react'
import './BigAd.css'
import ad1 from './ad1.jpg'
import ad2 from './ad2.jpg'
import ad3 from './ad3.jpg'
import ad4 from './ad4.png'
export default function BigAd() {
    const [Image, setImage] = useState(ad1)
    const ChangeImage = async (Image)=>{
       setImage(Image);
    }
  return (
    <div>
         <div className="container">
        <img id='BannerAd' src={`${Image}`} alt="Ad"/>
        <button onClick={() => ChangeImage(ad1)}>button 1</button>
        <button onClick={() => ChangeImage(ad2)}>button 2</button>
        <button onClick={() => ChangeImage(ad3)}>button 3</button>
        <button onClick={() => ChangeImage(ad4)}>button 4</button>
        
        </div> 

    </div>
  )
}

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

相关问题 使用React.js添加按钮时出错 - Getting error while adding buttons using React.js 反应.js。 当我尝试使用 FileReader 上传超过 3 个图像文件时,仅上传了 3 个 - React.js . when i am trying to upload more then 3 image files, using FileReader, only 3 uploaded 从 React.js 中的 @material-ui/pickers 选择日期后出现错误 - i am getting error after i selecting the date from @material-ui/pickers in React.js 在React.js中使用函数时尝试更改状态 - Trying to change state while using a function in React.js 我正在尝试下载React js,但是又一次又一次出现此错误? - I am trying to download React js, but I am getting this error again and again? 我在 react.js 中收到 400 响应错误我知道后端没问题但客户端我不确定我不熟悉反应 - i`m getting the 400 response error in react.js i know the backendd is ok but the client side i am not sure i am not fimiliar with react 需要帮助解决我不断收到的 react.js 错误 - Need help on a react.js error that I keep on getting 使用React.js显示消息时出错 - Getting error while displaying the message using React.js 图像单击我给了一个功能,单击它获取图像ID,使用它我必须使用javascript更改图像 - to image click I gave a function, on clicking it am getting image id, using this I have to change the image using javascript react.js图像没有加载 - react.js image is not getting loaded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM