A
A
Alexey Mikhalev2021-09-04 23:18:41
React
Alexey Mikhalev, 2021-09-04 23:18:41

Where does the empty object in the prop come from?

Greetings. Started learning React, trying to pass objects between components. Initially, props is empty, which is logical. Next, I pass objects and they seem to be added, but the empty prop object remains. In articles / YouTube, I did not observe such a problem. I am attaching the code, I hope you can help me. Thank you.

import React, { useState } from 'react'
import Header from './Todo/header'
import Task from './Todo/task'
import "./index.css";

function App() {
  const [posts, setPosts] = useState([
    {id: 1, text: "Текст-1"},
    {id: 2, text: "Текст-2"},
    {id: 3, text: "Текст-3"}
  ])
  
  return (
    <div>
      <Header/>
      {posts.map((post) =>
        <Task post={post} key={post.id} />
        ) }
      <Task/>
    </div>
  );
}
export default App;

import React from 'react'

export default function Task(props){
   console.log(props)
return(
            <div className="wrapper">
                <div className="task-data">
                    <div className="id">
                        <font className="text-id">
                           Текст              
                        </font>
                    </div>
                    <div className="id">
                        <font className="text-id">
                            Текст
                        </font>
                    </div>
                </div>
            </div>
    )
}

6133d3fc3ca5d036184124.jpeg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WbICHA, 2021-09-04
@mikkhalev

Indeed, from where?

<div>
      <Header/>
      {posts.map((post) =>
        <Task post={post} key={post.id} />
        ) }
>     <Task/>
    </div>

F
frontendprof, 2021-09-06
@frontendprof

Aleksey Mikhalev
You called the Task component twice, and you didn't pass props on the second call.
You'd better remove the second Task component and everything will work fine. :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question