1

I'm very new to react so I'm just following youtube video but I got this error and I don't know anything about this. Terminal showing 'Compiled Successfully' but I'm getting this error. This is my App.js code

import Todos from './components/Todos';
import './App.css';

function App() {
  React.state = {
    todos: [
      {
        id: 1,
        title: 'Pay the rent',
        completed: false
      },
      {
        id: 2,
        title: 'Dinner at Pappu',
        completed: false
      },
      {
        id: 3,
        title: 'CODROBO Meeting',
        completed: false
      }
    ]
  };
  return (
    <div className='App'>
      <Todos todos={this.state.todos} />
    </div>
  );
}

export default App;

and this is my Todos.js code


function Todos() {
  return this.props.todos.map(todo => <h3> {todo.title} </h3>);
}

export default Todos;

According to video lecture browser must be showing all the todos but I'm getting this error

1
  • 3
    You are creating a functional component and trying to access this property, which is the property of class component. so this.state will throw error. Commented Feb 11, 2020 at 7:10

5 Answers 5

2

As @Akhil Aravind pointed out you are trying to access this in functional component, which is the property of class. Try this:

import React, { Component } from 'react';
import Todos from './components/Todos';
import './App.css';

class App extends Component {
  state = {
    todos: [
      {
        id: 1,
        title: 'Pay the rent',
        completed: false
      },
      {
        id: 2,
        title: 'Dinner at Pappu',
        completed: false
      },
      {
        id: 3,
        title: 'CODROBO Meeting',
        completed: false
      }
    ]
  };
render(){
return (
    <div className='App'>
      <Todos todos={this.state.todos} />
    </div>
  );
}

}

export default App;

function Todos(props) {
  return props.todos.map(todo => <h3> {todo.title} </h3>);
}

export default Todos;

Hope this solves.

Sign up to request clarification or add additional context in comments.

7 Comments

please update the app component also, there are sill issues in the app component. He is trying to access this.state, which still throws error
It will not solve it, and it's not even answering the question he had. He has problems with state of undefined, not props of undefined, look in the first component, there he is passing the props with this.state, and using state like you would in a classic class component, and not through functional components hooks. That is what is giving him the error message he asks for
did not saw that
@AkhilAravind I know, I was commenting towards the post itself, and the question of why it will not solve it, but with a bit more details
@Rojen this does not qualify as an answer, nowhere is mentioned that todos comes from the props. See answer from Sodnarts
|
2

You are using a functional component, whilst using the syntax of a class component for state. This will not work.

There is two options for fixing this:

First one would be the easiest since most of your code is written with class component syntax -- Change it to a class component.

class App extends React.Component {
  state = {
    todos: [
      {
        id: 1,
        title: 'Pay the rent',
        completed: false
      },
      {
        id: 2,
        title: 'Dinner at Pappu',
        completed: false
      },
      {
        id: 3,
        title: 'CODROBO Meeting',
        completed: false
      }
    ]
  };

   render() {
      return (
         <div className='App'>
             <Todos todos={this.state.todos} />
         </div>
      );
   }
}

export default App;

Or you can change the state to the one of a functional component:

App.js

import Todos from './components/Todos';
import './App.css';

function App() {
  const [todos, setTodos] = React.useState([
      {
        id: 1,
        title: 'Pay the rent',
        completed: false
      },
      {
        id: 2,
        title: 'Dinner at Pappu',
        completed: false
      },
      {
        id: 3,
        title: 'CODROBO Meeting',
        completed: false
      }
    ])
  return (
    <div className='App'>
      <Todos todos={todos} />
    </div>
  );
}
export default App;

Todos.js

function Todos({todos}) {
  return todos.map(todo => <h3> {todo.title} </h3>);
}

export default Todos;

Using these two above files should resolve your issue completely.

2 Comments

The same error is occurring again.I'm literally very confused. Please explain again.
Hi, I added the Todos.js, if you use both the component marked as App.js, and Todos.js as stated above it works perfectly, I have tested it
1

Either you pass props or directly use destructuring.

// using props if it's functional component
function Todos(props) {
  return props.todos.map(todo => <h3> {todo.title} </h3>);
}

or

// using destructuring
   function Todos({todos}) {
    return todos.map(todo => <h3> {todo.title} </h3>);
  }

if it's functional component use props.todos. if class based component use this.props.todos

As i can see - you need to define hooks or class based component to define state

// Hooks
function App() {
  const [todos, setTodos] = React.useState([
      {
        id: 1,
        title: 'Pay the rent',
        completed: false
      },
      {
        id: 2,
        title: 'Dinner at Pappu',
        completed: false
      },
      {
        id: 3,
        title: 'CODROBO Meeting',
        completed: false
      }
    ])
  return (
    <div className='App'>
      <Todos todos={todos} />
    </div>
  );
}

Class based

class App extends React.Component {
  state = {
    todos: [
      {
        id: 1,
        title: "Pay the rent",
        completed: false
      },
      {
        id: 2,
        title: "Dinner at Pappu",
        completed: false
      },
      {
        id: 3,
        title: "CODROBO Meeting",
        completed: false
      }
    ]
  };
  render() {
    return (
      <div className="App">
        {this.state.todos.map(todo => (
          <h3>{todo.title}</h3>
        ))}
      </div>
    );
  }
}

4 Comments

That would fix one of the issues. But it is not the error he is asking for, but the one that would appear after fixing the problem he asked for. He is asking for state of undefined, which is where ha passes the props, since that also is a functional component, and he is using this.state there.
When I commented, I did not mean copy-paste my answer into yours.
@Sodnarts excuse me i didn't see your solution even comments ok.
using destructing really worked. Thanks @VahidAkhtar
1

from your program, there seems to be a slight error in the writing state. you can see in my example down below or in here. I hope this helps :)

// App.js
import React, { useState } from "react";
import Todos from "./Todos";

export default function App() {
  const [todos] = useState([
    {
      id: 1,
      title: "Pay the rent",
      completed: false
    },
    {
      id: 2,
      title: "Dinner at Pappu",
      completed: false
    },
    {
      id: 3,
      title: "CODROBO Meeting",
      completed: false
    }
  ]);
  return (
    <div className="App">
      <Todos todos={todos} />
    </div>
  );
}

// Todos.js
import React from "react";

function Todos({ todos }) {
  return (
    <React.Fragment>
      {todos.map((item, i) => (
        <div
          style={{
            background: "blue",
            margin: "10px",
            padding: "10px",
            color: "white"
          }}
          key={i}
        >
          <p
            style={{
              background: "red",
              width: "18px",
              borderRadius: "50%",
              padding: "5px"
            }}
          >
            {item.id}
          </p>
          <h3>{item.title}</h3>
        </div>
      ))}
    </React.Fragment>
  );
}

export default Todos;

Comments

0

In your Todos function you need to have your props from App and also since Todos is is a functional component, you don't need to use this keyword

function Todos(props) {
      return props.todos.map(todo => <h3> {todo.title} </h3>);
    }
    
    export default Todos;
<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>

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.