M
M
mosikus2019-08-08 23:12:48
JavaScript
mosikus, 2019-08-08 23:12:48

How to include your javascript in react?

Inside react

<input id="txtF" type="number" onKeyPress="return check(event,value)" onInput="checkLength(3,this)" />
 
<input  type="number" onKeyPress="return check(event,value)" onInput="checkLength(4,this)" />
   
<input  type="number" onKeyPress="return check(event,value)" onInput="checkLength(5,this)" />

I create a code.js file with code and export it
function checkLength(len,ele){
  var fieldLength = ele.value.length;
  if(fieldLength <= len){
    return true;
  }
  else
  {
    var str = ele.value;
    str = str.substring(0, str.length - 1);
    ele.value = str;
  }
}

export default checkLength;

In react I write import
import code from './code.js';
But this does not work (the limit in the input on the number of characters should work). Tell me, please, I'm completely nuts.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
i1yas, 2019-08-08
@mosikus

onInput="checkLength(3,this)"
jsx is not html. Event Handling in JSX . And in general, you need to read more about JSX.
Strange use of this, it will not point where it is needed, if the DOM element is so needed, then you need to pull it out of the event:

<input id="txtF" type="number" onInput={e => checkLength(3, e.target)} />

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question