简体   繁体   English

示例代码,错误:应为“;” 但发现“类”和更多语法错误

[英]Code from example, error: Expected “;” but found “class” and more syntax errors

In new to React, and I'm trying to understand examples from the Web.在 React 新手中,我试图从 Web 中理解示例。 Could You tell me what is wrong with that code?你能告诉我那个代码有什么问题吗?

class MyComponent extends React.component {
constructor() {
    this.state = {

        counter: 0,
        items: [1, 2]
    };
}
increase(this) {
    this.state.counter++;
}
addItem() {
    this.state.items = this.state.items.push(this.state.items.length + 1);
}
render() {
    <
    div class = ”data” >
        <
        div > {
            this.state.counter
        } < /div> <
        ul > {
            this.state.items.map(item => ( <
                li key = {
                    item
                } > element {
                    item
                } < /li>
            ))
        } <
        /ul> <
        /div> <
        div class = ”actions” >
        <
        button type = ”button” onclick = {
            this.increase
        } > zwiększ < /button> <
        button type = ”button” onclick = {
            this.addItem
        } > dodaj < /button> <
        /div>
}

} ReactDOM.render( < MyComponent >, document.querySelector("body")); } ReactDOM.render(<MyComponent>, document.querySelector("body"));

Notes:笔记:

  • methods must bind to class方法必须绑定到 class
  • do not directly mutate state, use setState不要直接变异 state,使用 setState
  • render must have a return渲染必须有回报
  • must return a single outer wrap or use fragment必须返回单个外包装或使用片段
  • class is className in react class 是反应中的类名
  • onclick is onClick in react onclick 是 onClick 在反应
  • don't inject directly into body, create a root div不要直接注入body,创建一个root div

Check out the working code snippet below:查看下面的工作代码片段:

 class App extends React.Component { constructor(props) { super(props); this.state = { counter: 0, items: [1, 2] }; } //methods must bind to class increase = () => { //do not directly mutate state, use setState this.setState(state => ({ counter: state.counter + 1 })); }; //methods must bind to class addItem = () => { //do not directly mutate state, use setState this.setState(state => ({ items: [...state.items, state.items.length + 1] })); }; render() { //render must have a return //must return a single outer wrap or use fragment //class is className in react //onclick is onClick in react return ( < React.Fragment > < div className = "data" > < div > { this.state.counter } < /div> < ul > { this.state.items.map(item => ( < li key = { item } > element { item } < /li> )) } < /ul> < / div > < div className = "actions" > < button type = "button" onClick = { this.increase } > zwiększ < /button> < button type = "button" onClick = { this.addItem } > dodaj < /button> < / div > < /React.Fragment> ); } } //don't inject directly into body, create a root div ReactDOM.render( < App / >, document.getElementById("root"))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root"></div>

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

相关问题 “语法错误:预期名称,找到 \”)\“.”, - “Syntax Error: Expected Name, found \”)\“.”, 处理错误“语法错误; 但发现我” - Processing error “Syntax error expected ; but found i” 使用示例代码进行JQuery选择时的语法错误 - Syntax errors when using example code for JQuery Chosen 如何解决以下错误“”语法错误:预期名称,找到<eof> "</eof> - how to solve the following error “”Syntax Error: Expected Name, found <EOF>" 在MDN网站上尝试示例时出现语法错误 - Syntax error on trying an example from MDN website GraphQL 导致 GraphQLError 的突变:语法错误:需要“:”,找到“}” - GraphQL Mutation causing GraphQLError: Syntax Error: Expected ":", found "}" JavaScript 中的逗号运算符:预期语法错误,但代码运行正常 - Comma operator in JavaScript: syntax error expected, but code runs OK VSCode语法错误; 预期 - VSCode Syntax Error ; expected 在找到的示例代码中,Array.prototype.find 的箭头 function 回调中令人惊讶的语法是什么? - Within a found example code, what is the surprising syntax in an arrow function callback of Array.prototype.find for? 与教程完全相同的代码,不断给我语法错误 - The exact same code from a tutorial that keeps giving me syntax errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM