gpt4 book ai didi

javascript - React 示例网页的控制台错误 - 有状态组件

转载 作者:行者123 更新时间:2023-12-01 03:11:22 26 4
gpt4 key购买 nike

代码片段可在 react webpage 上找到。

向下滚动到有状态组件

复制+粘贴到我的codepen中,出现此控制台错误

Uncaught SyntaxError: Unexpected token <

这是codepen link

这也是代码本身:

class Timer extends React.Component {
constructor(props) {
super(props);
this.state = {secondsElapsed: 0};
}

tick() {
this.setState((prevState) => ({
secondsElapsed: prevState.secondsElapsed + 1
}));
}

componentDidMount() {
this.interval = setInterval(() => this.tick(), 1000);
}

componentWillUnmount() {
clearInterval(this.interval);
}

render() {
return (
<div>Seconds Elapsed: {this.state.secondsElapsed}</div>
);
}

}
ReactDOM.render(<Timer />, mountNode);

最佳答案

Add Babel as preprocessor, add node in HTML where you wish to mount the component.

<div id="mountNode"/>

更新链接:https://codepen.io/DP888/pen/Gvdwzg?editors=1010

As the JSX syntax and ES6, are not supported in all the browsers.Hence, if we are using them in the React code, we need to use a tool which translates them to the format that has been supported by the browsers. It’s where babel comes into the picture.

关于javascript - React 示例网页的控制台错误 - 有状态组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45821987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com