javascript - React 中的函数应该放在哪里?
问题描述
抱歉这个愚蠢的问题,但我寻找并寻找答案,但没有任何东西对我有用。我有一个将 className 添加到我的导航栏的 handleClick 函数,因此当它缩小时,它会删除链接并出现三行。但是当我添加我的函数时,React 无法编译并抛出这个错误:
./src/App.js
Syntax error: Unexpected token, expected ";" (25:11)
23 | x.className = "brand";
24 | }
> 25 | render(){
| ^
26 | const carrot = <FontAwesomeIcon icon={faCarrot} />
27 | return (
28 | <HashRouter>
请帮忙。这是我的代码:
import React, { Component } from 'react';
import {
Route,
NavLink,
HashRouter
} from "react-router-dom";
import Home from "./home";
import Stuff from "./stuff";
import Contact from "./contact";
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCarrot } from '@fortawesome/free-solid-svg-icons';
class App extends Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
var x = document.getElementById("myTopnav");
if (x.className === "brand") {
x.className += " responsive";
} else {
x.className = "brand";
}
render(){
const carrot = <FontAwesomeIcon icon={faCarrot} />
return (
<HashRouter>
<div className="wrapper">
<div className="brand" id="myTopnav">
<a href="#"><h1>{carrot}Plan your meal</h1></a>
<a href="#"><h3 className="login">Log In</h3></a>
<a href="#"><h3 className="signup">Sign Up</h3></a>
<a href="javascript:void(0);" className="icon" onClick={this.handleClick}>☰</a>
</div>
<div>
<ul className="header">
<li><NavLink exact to="/" href="/">Home</NavLink></li>
<li><NavLink to="/stuff">Single meal</NavLink></li>
<li><NavLink to="/contact">Whole day</NavLink></li>
</ul>
</div>
<div className="content">
<Route exact path="/" component={Home}/>
<Route path="/stuff" component={Stuff}/>
<Route path="/contact" component={Contact}/>
</div>
</div>
</HashRouter>
);
}
}
export default App;
这是我的 GitHub 存储库: https ://github.com/Meret12/Planyourmeal
解决方案
你错过了一个右括号: -
class App extends Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
var x = document.getElementById("myTopnav");
if (x.className === "brand") {
x.className += " responsive";
} else {
x.className = "brand";
}
}
render() {
const carrot = <FontAwesomeIcon icon={faCarrot} />;
return (
<HashRouter>
<div className="wrapper">
<div className="brand" id="myTopnav">
<a href="#"><h1>{carrot}Plan your meal</h1></a>
<a href="#"><h3 className="login">Log In</h3></a>
<a href="#"><h3 className="signup">Sign Up</h3></a>
<a href="javascript:void(0);" className="icon" onClick={this.handleClick}>☰</a>
</div>
<div>
<ul className="header">
<li><NavLink exact to="/" href="/">Home</NavLink></li>
<li><NavLink to="/stuff">Single meal</NavLink></li>
<li><NavLink to="/contact">Whole day</NavLink></li>
</ul>
</div>
<div className="content">
<Route exact path="/" component={Home}/>
<Route path="/stuff" component={Stuff}/>
<Route path="/contact" component={Contact}/>
</div>
</div>
</HashRouter>
);
}
推荐阅读
- mongodb - 有没有办法对mongodb中的列顺序进行排序?
- r - 如何在 R 中循环使用两个列表
- c# - 如何从引用库的项目中获取变量?
- google-cloud-platform - (GCloud SQL) 有没有办法通过 CLI 或 API 选择加入维护通知?
- typescript - 如何在运行时将参数传递给 TypeScript 装饰器?
- amazon-web-services - CloudFormation - 使用 ->- 表达式和 !Sub
- python-3.x - 在其他文件中导入文件时 pychram 出错
- sql-server - DbCreator 角色可以恢复数据库,但不能在 MSSQL Server 中访问它
- date - 如何使用周值配置单元获取月值
- ruby-on-rails - 如何在 Ruby on Rails 5.2 中使用原始 Postgres SQL 查询结果?