-1

This is my code, but I can't get it to work, I'm using react and typescript(I'm new to both), i don't know what I'm doing wrong, it always says: Uncaught TypeError: Cannot read properties of null(reading 'value'). What did I do wrong?

import { Button } from "./components/ui/button"
import { Input } from "./components/ui/input"

function App() {
  const inputElement = document.getElementById('bla') as HTMLInputElement.value;
  console.log(inputElement)

  return (
    <>
    <div className="p-6 space-y-6">
      <div className="flex justify-center">
        <div className="flex justify-between w-1/2">
          <h1 id="bla">bla bla bla</h1>
          <Input id="pokemonInput" placeholder="Pokemon name" className="border rounded w-1/2"/>
          <Button>Pesquisar</Button>
        </div>
      </div>

      <div className="flex justify-center">
        <img src="" id="pokemonImage" className="border rounded w-1/2"/>
      </div>
    </div>
    <script type="module" src="./src/script.ts"></script>
    </>
  )
}



export default App

It should get the string 'bla bla bla' and console.log it but I can't get the property value, no matter what.

4
  • It can't find the element because the element isn't on the page when that line of code executes. It's not clear why you'd even want to do this, and looks like you should really be starting with some introductory tutorials on React to learn how to use it. (Or just don't use it for whatever you're trying to do here.) Commented Feb 11, 2024 at 22:28
  • 1
    You're missing brackets (document.getElementById('bla') as HTMLInputElement)?.value Commented Feb 11, 2024 at 22:28
  • If you are trying to get the value of the text field or other input from within react button click code you can look into useRef - simple example in this answer Commented Feb 11, 2024 at 22:30
  • ^- What nate-kumar says, to which I want to add: bla is not the id of your <Input> element. Voting to close as "Typo". Commented Feb 11, 2024 at 23:36

3 Answers 3

0

You are trying to get the 'value' property on a h1 element, which doesn't have this type of property. To get the content of the h1 element, you can set in a const h1 = document.getElementById('bla'); and then use another constant to put the property '.textContent' as const h1Value = h1?.textContent;

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

Comments

0

React is not recommended to affect the DOM without using React as much as possible.

You can use useRef hook for get value of H1. You can implement the useRef like below.

const inputElement = useRef(null);

return (
  <>
    <h1 ref={inputElement}>bla bla bla</h1>
    <Input
      onChange="handleChange"
      id="pokemonInput"
      placeholder="Pokemon name"
      className="border rounded w-1/2"
    />
    <Button>Pesquisar</Button>
  </>
);

And then, you can get the value like inputElement.current?.value

But I'd like to recommend you to use states in React.

States are simple to use it.

const [inputElement, setInputElement] = useState("bla bla bla");

function handleChange(e) {
  setInputElement(e.target.value);
}

return (
  <>
    <h1>{inputElement}</h1>
    <Input
      onChange="handleChange"
      id="pokemonInput"
      placeholder="Pokemon name"
      className="border rounded w-1/2"
    />
    <Button>Pesquisar</Button>
  </>
);

1 Comment

I used states and it worked, thanks :D
0
import { useRef, FormEvent } from "react";

function App()
{

const h1Ref = useRef<HTMLHeadingElement>(null)

const handleSubmit = (e:FormEvent<HTMLFormElement>)=>{
  e.preventDefault()
  console.log(h1Ref.current?.innerText)
}

  return (
    <>
    <div className="p-6 space-y-6">
      <div className="flex justify-center">
        <form onSubmit={handleSubmit} className="flex justify-between w-1/2">
          <h1 ref={h1Ref} id="bla">bla bla bla</h1>
          <input id="pokemonInput" placeholder="Pokemon name" className="border rounded w-1/2"/>
          <button type="submit">Pesquisar</button>
        </form>
      </div>

      <div className="flex justify-center">
        <img src="" id="pokemonImage" className="border rounded w-1/2"/>
      </div>
    </div>
    {/* <script type="module" src="./src/script.ts"></script> */}
    </>
  )
}

export default App

There are a few changes I have made to your code:

  1. I have used useRef Hook instead of the document.getElementById. React.js was created to deal with the issue of having to manually deal with the real DOM that's why it uses the virtual DOM and the tree reconciliation algorithm before it goes to the real DOM to tear down things. React has a nice hook called useRef that allows you to reference DOM elements easily. useRef

const h1Ref = useRef<HTMLHeadingElement>(null) the <HTMLHeadingElement> is to tell typescript the type of ref it will be used on.

  1. As you stated you wanted to log the inner text of the h1 so I have provided a function that logs it when you submit the form
const handleSubmit = (e:FormEvent<HTMLFormElement>)=>{
  e.preventDefault()
  console.log(h1Ref.current?.innerText)
}
  1. You don't have to include your scripts that way. Your scripts should be in the index.html file. In case you want to add other scripts, add them to the index.html file as that serves as the root to your "ui".

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.