【发布时间】:2020-07-30 08:39:18
【问题描述】:
我对反应还很陌生,想知道如何更改下面的代码,以便为每个部分标记使用 onClick 函数。
render(){
return(
<div className='App'>
< Header />
<div className="secHeader"><h2>General Education Core</h2></div>
<br/>
{/* Displays the 6 different gen eds cateogry that uic offers and calls the displaygenEdCourse component and sends props to know which item was clicked */}
<div className="box alt container">
{/* link for Analyzing the Natural World */}
<section className="feature left">
<Link to={{pathname: "/displayGenEdCourses", state: { linkState: 'ANW' }}} className="image icon solid fa-seedling"><img src={pic01} alt="pic01" /></Link>
<div className="content">
<h3>Analyzing the Natural World</h3>
</div>
</section>
{/* link for Understanding the Individual and Society */}
<section className="feature right">
<Link to={{pathname: "/displayGenEdCourses", state: { linkState: 'UIS' }}} className="image icon solid fa-users"><img src={pic01} alt="pic01" /></Link>
<div className="content">
<h3>Understanding the Individual and Society</h3>
</div>
</section>
{/* link for Understanding the Past */}
<section className="feature left">
<Link to={{pathname: "/displayGenEdCourses", state: { linkState: 'UP' }}} className="image icon solid fa-history"><img src={pic01} alt="pic01" /></Link>
<div className="content">
<h3>Understanding the Past</h3>
</div>
</section>
{/* link for Understanding the Creative Arts */}
<section className="feature right">
<Link to={{pathname: "/displayGenEdCourses", state: { linkState: 'UCA' }}} className="image icon solid fa-palette"><img src={pic01} alt="pic01" /></Link>
<div className="content">
<h3>Understanding the Creative Arts</h3>
</div>
</section>
{/* link for Exploring World Cultures */}
<section className="feature left">
<Link to={{pathname: "/displayGenEdCourses", state: { linkState: 'EWC' }}} className="image icon solid fa-globe"><img src={pic01} alt="pic01" /></Link>
<div className="content">
<h3>Exploring World Cultures</h3>
</div>
</section>
{/* link for Understanding U.S. Society */}
<section className="feature right">
<Link to={{pathname: "/displayGenEdCourses", state: { linkState: 'UUS' }}} className="image icon solid fa-flag-usa"><img src={pic01} alt="pic01" /></Link>
<div className="content">
<h3>Understanding U.S. Society</h3>
</div>
</section>
</div>
< Footer />
</div>
)
}
每个部分标记都是我希望能够单击的块/图像,当我单击它时,它应该调用 onClick 函数,以便我通知单击了哪个部分。我尝试将 onClick 放在像<section className="feature left" onClick={console.log('testing')}> 这样的部分标签内。还尝试将 onClick 放置在每个部分标签的其他位置,但它似乎不起作用。每当页面呈现时,都会调用 onClick,然后当我单击该图像时,它不会调用 onClick。不知道哪里出错了。
【问题讨论】:
标签: javascript html reactjs