T
T
ThomasScott2021-02-06 17:12:56
React
ThomasScott, 2021-02-06 17:12:56

Where are the errors in the 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"));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anvar Shakhmaev, 2021-02-06
@ThomasScott

here:

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);
    }

Where a constructor is neededsuper(props);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question