How to make a component with a function

Asked

Viewed 36 times

-1

PURE CODE

<input type="text" id="nf" placeholder="Nota Fiscal" />
<input type="text" id="cnpj" placeholder="CNPJ" />
<input type="button" value="Buscar" onclick="buscarNF()" />

<script type="text/javascript">
    function buscarNF(){
        var nf = document.getElementById('nf').value;
        var cnpj = document.getElementById('cnpj').value.replace(/[^0-9]/g, '');
        window.location = 'https://web.bsoft.com.br/rastrear/notafiscal/wy4R8qaVcWgaKnFS/'+ nf+'/'+cnpj;
    }
</script>

I tried to do in 2 files, but in vscode it says "Compiled successfully", but in the browser it shows errors

GALLERY JS ARCHIVE

import buscarNF from '../util'

export const Gallery = () =>{  
  return( 
      <buscarNF>
       <form>
         <label>
           <input type="text" id="nf" placeholder="Nota Fiscal" />
           <input type="text" id="cnpj" placeholder="CNPJ" />
           <input type="button" value="Buscar" onClick={buscarNF()}/>
         </label>
       </form>
     </buscarNF>)
 }

UTIL JS FILE

function buscarNF(props){  
    const nf = document.getElementById('nf').value;
    const cnpj = document.getElementById('cnpj').value.replace(/[^0-9]/g, '');
    window.location = 'https://web.bsoft.com.br/rastrear/notafiscal/wy4R8qaVcWgaKnFS/'+ nf+'/'+cnpj;
}

export default buscarNF;
  • 2

    You are using React, so what is the reason to use document.getElementById?. You would be using imperative paradigm that is opposed to the declarative paradigm of React. buscarNF is a function and not a component, so I did not understand why <buscarNF>

  • So I am very new and I still do not have much notion need to include on my site this function so it was passed to me this form of pure html and I do not know turn into a Retact

  • Okay, the simple tip I give is to create 2 States in Gallery, that will receive the values of the inputs and the function buscarNF takes as argument the value of these two inputs and makes the redirect. And Remove <buscarNF>

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.