0

I'm working through the Tania Rascia React tutorial in an attempt to learn. I ran into a point where I have several errors showing up. The solution to them seems obvious, but I'm just confused if there is something I am missing, since this is the exact way the tutorials code is. This tutorial is officially endorsed by react, which is why I'm baffled. Am I doing something wrong? Or is the tutorial wrong?

Here are the errors:

Line 7: 'state' is not defined no-undef Line 27: 'removeCharacter' is not defined no-undef Line 39: 'characters' is not defined no-undef

and here is the code:

 //  eslint-disable-next-line
import React, { Component } from 'react';
import Table from './Table.js';

class App extends Component {
    render(){
        state = {
            characters:[
                            {
                'name': 'Charlie',
                'job': 'Janitor'
            },
            {
                'name': 'Mac',
                'job': 'Bouncer'
            },
            {
                'name': 'Dee',
                'job': 'Aspiring Actress'
            },
            {
                'name': 'Dennis',
                'job': 'Bartender'
            }
            ]
        };
    removeCharacter = index => {
        const { characters } = this.state;

        this.setState({
            characters: characters.filter((character, i) => {
                return i !== index;
            })
        });
    }

        return (
            <div className="container">
                <Table characterData={characters} />
            </div>
        );
    }
}

export default App;
1

5 Answers 5

3

you seem to lack basic React concept please read react documentation along with your video tutorial.

import React, { Component } from 'react';
import Table from './Table.js';

class App extends Component {
     state = {
            characters:[
                            {
                'name': 'Charlie',
                'job': 'Janitor'
            },
            {
                'name': 'Mac',
                'job': 'Bouncer'
            },
            {
                'name': 'Dee',
                'job': 'Aspiring Actress'
            },
            {
                'name': 'Dennis',
                'job': 'Bartender'
            }
            ]
        };

     removeCharacter = index => {
      const { characters } = this.state;

        this.setState({
            characters: characters.filter((character, i) => {
                return i !== index;
            })
        });
     }

    render(){
      const { characters } = this.state;
      return (
            <div className="container">
                <Table characterData={characters} />
            </div>
        );
    }
}

export default App;
Sign up to request clarification or add additional context in comments.

Comments

2

@masmerino provided the correct answer. But to anyone who may view this post, the line 39 error was only solved after changing

 <Table characterData={characters} />

to this:

 <Table characterData={this.state.characters} />

1 Comment

For me, this is the correct answer: when following along the tutorial (and code all in the correct place!), you still get an error if you just use the code supplied in the tutorial. Was doing my head in, cheers.
1

Your code is misplaced.

return comes inside render and state and handlers comes outside the render.

Corrected code :

import React, { Component } from 'react';
import Table from './Table.js';

class App extends Component {

state = {
            characters:[
                            {
                'name': 'Charlie',
                'job': 'Janitor'
            },
            {
                'name': 'Mac',
                'job': 'Bouncer'
            },
            {
                'name': 'Dee',
                'job': 'Aspiring Actress'
            },
            {
                'name': 'Dennis',
                'job': 'Bartender'
            }
            ]
        };
    removeCharacter = index => {
        const { characters } = this.state;

        this.setState({
            characters: characters.filter((character, i) => {
                return i !== index;
            })
        });

    render(){
         return (
            <div className="container">
                <Table characterData={characters} />
            </div>
        );
    }


    }
}

export default App;

2 Comments

God I'm dumb. Thanks for dealing with my idiocy :)
happy to help you
0

state and removeCharacter have to be out of your render function

class App extends Component {
    state = {
        characters:[
                        {
            'name': 'Charlie',
            'job': 'Janitor'
        },
        {
            'name': 'Mac',
            'job': 'Bouncer'
        },
        {
            'name': 'Dee',
            'job': 'Aspiring Actress'
        },
        {
            'name': 'Dennis',
            'job': 'Bartender'
        }
        ]
    };

    removeCharacter = index => {
        const { characters } = this.state;

        this.setState({
            characters: characters.filter((character, i) => {
                return i !== index;
            })
        });
    }

    render() {
        return (
            <div className="container">
                <Table characterData={characters} />
            </div>
        );
    }
}

export default App;

Comments

0

I am currently following the same Tania tutorial and it gave me the error that removeCharacter was not declared and it was because I was writing it after RENDER. here is the code without errors

import React, {Component} from 'react'
import Table from './Table'


class App extends Component{
  state= {
    characters: [
      {
        name: 'Charlie',
        job: 'Janitor',
      },
      {
        name: 'Mac',
        job: 'Bouncer',
      },
      {
        name: 'Dee',
        job: 'Aspring actress',
      },
      {
        name: 'Dennis',
        job: 'Bartender',
      },
    ],
  }

  removeCharacter = (index) => {
    const {characters} = this.state
  
    this.setState({
      characters: characters.filter((characters, i) => {
        return i !== index
      }),
    })
  }

  render(){
    const { characters } = this.state

    return(
      <div className="container">
      <Table  characterData={characters} removeCharacter={this.removeCharacter}/>
      </div>
    )
  }
}

export default App

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.