Hi everyone,I have some issues with my React Code and i can't use the ReactDom because the console says that 'Uncaught TypeError: ReactDOM is undefined'. This is my code and begins with my first component AddTasks :
import { useState } from "react"
import ShowOrDelete from "../ShowOrDelete"
import React from 'react'
const AddTasks = () =>{
const [tasks, setTasks] = useState('')
const placeHolderText = "Escribe algo aqui"
return(
<div className="bg-slate-200 p-8 mt-36 m-auto w-1/2 h-96 rounded-lg text-2xl text-center" id="task-container">
<h2 className="mb-8"><strong>Mis Tareas</strong></h2>
<div>
<input className="h-10" type="text" name="tasks" id="input-value" placeholder={placeHolderText}/>
<ShowOrDelete insertTasks={setTasks} tasks={tasks}/>
</div>
</div>
)
}
export default AddTasks
Next this is the next Component that must to render and where i have the issue.
import React from 'react'
import ReactDOM from 'react-dom/client'
const ShowOrDelete = ({ insertTasks, tasks }) =>{
const addTasksToComplete = () =>{
const input = document.getElementById('input-value').value;
insertTasks(input)
const tasksDiv = <div className="bg-slate-950 text-white text-xl"><p>{tasks}</p></div>;
ReactDOM.render(
tasksDiv,
document.getElementById('task-container')
)
}
return(
<div className="inline">
<button onClick={() => addTasksToComplete(insertTasks, tasks)} className="bg-green-500 text-white text-xl p-2">Agregar Tareas</button>
</div>
)
}
export default ShowOrDelete
But then in the console of the localhost show me the message of the title. So what i can do to resolve this or what is the issues?
Only your
index.jsshould contain aReactDOM.render(), all deeper components should just render there UI by providing areturn().Same goes for
document.getElementById, you should only use that once inindex.jsto get the React root element, in deeper components, thosegetElement**function are almost always a bad idea