Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
how to solve in react Cannot set properties of null (setting 'inner HTML')?
import React, { useState } from "react";
export default function DropDown() {
  
const fun=()=>{
document.getElementById('id').innerHTML='Hello world'
}
  return (
    <>
    <button onClick={fun()}>click</button>
      <p id="id"></p>
    </>
  );
}

how to resolve the error in react? not in JavaScript. If we are not use inner HTML instead of that what to use in that field?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Your immediate problem is that you are calling fun immediately and then assigning its return value (undefined) to onClick.

To go about this the way you are trying, you need to assign the function itself.

onClick={fun}

However, direct DOM manipulation runs counter to the way React is designed to work.

Your changes are likely to get wiped out if anything triggers a re-render.

React is designed to be data driven. To change the DOM you should change the data and let React pull it through to the DOM itself.

You should make use of state to track the data.

export default function DropDown() {
  const [open, setOpen] = useState(false);
  const fun = useCallback(() => setOpen(open => !open), []);
  return (
    <>
    <button onClick={fun}>click</button>
    {open && (<p>Hello, world</p>)
    </>
  );
}
about 4 years ago · Santiago Trujillo Relatório

0

In React, using the State Hook

https://reactjs.org/docs/hooks-state.html

Your code should be

import React, { useState } from "react";
export default function DropDown() {
  
  const [greeting, setGreeting] = useState('Hello world');
  const onChange = () => {
    setGreeting('New greeting')
  }

  return (
    <>
    <button onClick={onChange}>click</button>
      <p id="id">{greeting}</p>
    </>
  );
}

about 4 years ago · Santiago Trujillo Relatório

0

This is because you are calling the fun function instead of binding it to the click.

It should be : <button onClick={fun}>click</button>

See this CodeSandbox

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda