2022前端开发社招React面试题 附答案
1:讲讲什么是 JSX ?
主题: React
难度: ⭐⭐⭐
当 Facebook 第一次发布 React 时,他们还引入了一种新的 JS 方言 JSX
,将原始 HTML 模板嵌入到 JS 代码中。JSX 代码本身不能被浏览器读取,必须使用Babel
和webpack
等工具将其转换为传统的JS。很多开发人员就能无意识使用 JSX,因为它已经与 React 结合在一直了。
class MyComponent extends React.Component {
render() {
let props = this.props;
return (
<div className="my-component">
<a href={props.url}>{props.name}</a>
</div>
);
}
}
2:根据下面定义的代码,可以找出存在的两个问题吗 ?
主题: React
难度: ⭐⭐⭐
请看下面的代码:
答案:
1.在构造函数没有将 props
传递给 super
,它应该包括以下行
constructor(props) {
super(props);
// ...
}
复制代码
2.事件监听器(通过addEventListener()
分配时)的作用域不正确,因为 ES6 不提供自动绑定。因此,开发人员可以在构造函数中重新分配clickHandler
来包含正确的绑定:
constructor(props) {
super(props);
this.clickHandler = this.clickHandler.bind(this);
// ...
}