started learning React js. I made a component so that it was output and so that when I entered into the input I would run the js function.
Code:
class Lable extends React.Component { render() { return ( <input type="text" id={this.props.id} placeholder=" " onChange={this.handleChange.bind(this)} /> ); } handleChange(e) { this.props.onSearch(e.target.id); } } ReactDOM.render( <p className="inp"> <label> <Lable name="1" id="roadFrom" onSearch={search} /> <span className="label" /> <span className="border" /> </label> </p>, document.getElementById("root") ); And a piece of the search function code, it performs an auto-complete imput, that is, I enter the first letters and it displays a list, etc., a live search.
function search(name) { var field = document.getElementById(name); var classInactive = "sf_inactive"; var classActive = "sf_active"; var classText = "sf_text"; var classSuggestion = "sf_suggestion"; field.c = field.className; field.className = field.c + " " + classInactive; field.onfocus = function() { this.className = this.c + " " + classActive; this.value = this.value == "" ? "" : this.value; }; field.onblur = function() { this.className = this.value != "" ? this.c + " " + classText : this.c + " " + classInactive; this.value = this.value != "" ? this.value : ""; clearList(); }; if (suggestion) { var selectedIndex = 0; field.setAttribute("autocomplete", "off"); var div = document.createElement("div"); var list = document.createElement("ul"); list.style.display = "none"; div.className = classSuggestion; list.style.width = field.offsetWidth + "px"; div.appendChild(list); field.parentNode.appendChild(div); } I naturally cut it, so as not to litter. My problem is that when I enter a character in an input, my list drops out, the class to the "sf_inactive" input is glued, but when I enter the second character, my div is not overwritten but a new one is pasted, naturally the old one hangs behind me. and the class "sf_inactive" is again glued to the input
This is what happens:
<input type="text" id="roadFrom" placeholder=" " class=" sf_inactive sf_inactive sf_inactive sf_text" autocomplete="off"> Here I have here already three times sf_inactive sf_inactive sf_inactive, since three crashes clicked on the button and three divs are glued
<div class="sf_suggestion"><ul tabindex="-1" style="display: block; width: 280px;"><li><a href="javascript:void(0);">sfsf</a></li></ul></div> <div class="sf_suggestion"><ul tabindex="-1" style="display: block; width: 280px;"><li><a href="javascript:void(0);">sfsf</a></li></ul></div> <div class="sf_suggestion"><ul tabindex="-1" style="display: block; width: 280px;"><li><a href="javascript:void(0);">sfsf</a></li></ul></div> If this is done not through react, but simply on the page, then everything works correctly, one class is added, and one div that is constantly changing.
How can react fix this? :(
appendChild. So, you are creating a new element, and where is the old one removed? - Misha Saidovsearchfunction - Misha Saidov