0
votes

I am new to react this is my first application. I am calling one component inside to another component then those function a moved to app.js

//app.js

   class App extends React.Component {
  state = {
    todos:[
      {id:1, title:'get haircut',completed: false},
      {id:2, title:'learn react',completed: false},
      {id:3, title:'chaaa',completed: false},
    ]
  }
  markComplete=(id) =>{
    this.setState({
      todos: this.state.todos.map((myTodo)=>{
        if(myTodo.id === id ){
          myTodo.completed = !myTodo.completed;
        }
        return myTodo
      })
    })
  };
  deleteTodo =(id) =>{
    this.setState({
      todos: [...this.state.todos.filter((myTodo) =>{
        return myTodo !==id
      })]
    })
  }
  render(){
  return (
    <div className="App">
      <Header/>
      <RetrivedTodos todos={this.state.todos}
      markComplete={this.markComplete}
      deleteTodo={this.deleteTodo}
      />
    </div>
  );
  }
}

//RetrivedTodos.js

class RetrivedTodos extends Component {
render () {
    return this.props.todos.map((retrivedTodos) =>(
        <TodosItems key={retrivedTodos.id} todos={retrivedTodos}
        markComplete={this.props.markComplete}
        deleteTodo={this.props.deleteTodo}
        />
    ))
}

}

//TodoItems.js

class TodosItems  extends Component {
getStrikeMark = () => {
    return {
        textDecoration:this.props.todos.Completed ? 'line-through': 'none'
    }
}
render () {
    const { id , title } = this.props.todos
    return (
        <div className='main-todos-div' style={this.getStrikeMark()}>
            <div className='todo-div'>
                <input type="checkbox" className='checkbox-round' 
                onChange={this.props.markComplete.bind(this,id)}/> 
                <span>{title}</span>
            </div>
            <div className='btn-div'>
                <button onClick={this.props.deleteTodo.bind(this,id)}>
                    <i className="fas fa-trash"></i>    
                 </button>
            </div>
        </div>
    )
}

} //header

class Header extends Component {
render () {
    const date= new Date();
    const todayDate = date.getDate();
    const month = date.toLocaleString('default',{month:'long'});
    const year = date.getFullYear;
    const day = date.toLocaleDateString('default',{weekday:'long'});
    return(
        <div className='main-header-div'>
                <div className='background-div'> </div>
                <div className='date-month-div'> </div>
                <span>{todayDate}</span>
                <span>{month}</span>
                <span>{year}</span>
                <span>{day}</span>
                </div>
    )
}

}

What is the problem here? It shows this error

Warning: Functions are not valid as a React child. This may happen if you return a Component instead of from render. Or maybe you meant to call this function rather than return it.

thanks in advance

2
Headers component code is missing.Asutosh
Asutosh i have added header component as wellFaizan Ahmad
Could you please add the whole component code? Seems like the imports are missingTrisma
I have added my answer, please check belowAsutosh

2 Answers

1
votes

Check the sandbox link:

https://codesandbox.io/s/affectionate-goodall-mh0t7?file=/src/Header.js

The problem is with Header componentnt, it should be :

 const year = date.getFullYear();

instead of

 const year = date.getFullYear;

getFullYear is a function, that's the reason you were getting the error.

0
votes

RetrivedTodos seems invalid to me. You are returning a map function instead of a React component. This map function should be executed inside the return value instead of being the return value itself.

Here is how it should look like:

class RetrivedTodos extends Component {
render () {
    return (
      <div>
        {this.props.todos.map((retrivedTodos) => (
          <TodosItems key={retrivedTodos.id} todos={retrivedTodos}
          markComplete={this.props.markComplete}
          deleteTodo={this.props.deleteTodo}
          />
        ))
       }
     </div>
   )
}

EDIT: Inside Header you are returning a function instead of it's value:

const year = date.getFullYear;

Should be:

const year = date.getFullYear();