React.js - input losing focus when rerendering

后端 未结 19 1917
猫巷女王i
猫巷女王i 2020-12-05 01:49

I am just writing to text input and in onChange event i call setState, so React rerenders my UI. The problem is that the text input always lose a f

19条回答
  •  有刺的猬
    2020-12-05 02:06

    I had the same problem with an html table in which I have input text lines in a column. inside a loop I read a json object and I create rows in particular I have a column with inputtext.

    http://reactkungfu.com/2015/09/react-js-loses-input-focus-on-typing/

    I managed to solve it in the following way

    import { InputTextComponent } from './InputTextComponent';
    //import my  inputTextComponent 
    ...
    
    var trElementList = (function (list, tableComponent) {
    
        var trList = [],
            trElement = undefined,
            trElementCreator = trElementCreator,
            employeeElement = undefined;
    
    
    
        // iterating through employee list and
        // creating row for each employee
        for (var x = 0; x < list.length; x++) {
    
            employeeElement = list[x];
    
            var trNomeImpatto = React.createElement('tr', null, {employeeElement['NomeTipologiaImpatto'].toUpperCase()});
            trList.push(trNomeImpatto);
    
            trList.push(trElementCreator(employeeElement, 0, x));
            trList.push(trElementCreator(employeeElement, 1, x));
            trList.push(trElementCreator(employeeElement, 2, x));
    
        } // end of for  
    
        return trList; // returns row list
    
        function trElementCreator(obj, field, index) {
            var tdList = [],
                tdElement = undefined;
    
            //my input text
            var inputTextarea = 
    
            tdElement = React.createElement('td', { style: null }, inputTextarea);
            tdList.push(tdElement);
    
    
            var trComponent = createClass({
    
                render: function () {
                    return React.createElement('tr', null, tdList);
                }
            });
            return React.createElement(trComponent);
        } // end of trElementCreator
    
    });
    ...    
        //my tableComponent
        var tableComponent = createClass({
            // initial component states will be here
            // initialize values
            getInitialState: function () {
                return {
                    impattiComposite: [],
                    serviceId: window.sessionStorage.getItem('serviceId'),
                    serviceName: window.sessionStorage.getItem('serviceName'),
                    form_data: [],
                    successCreation: null,
                };
            },
    
            //read a json data soure of the web api url
            componentDidMount: function () {
                this.serverRequest =
                    $.ajax({
                        url: Url,
                        type: 'GET',
                        contentType: 'application/json',
                        data: JSON.stringify({ id: this.state.serviceId }),
                        cache: false,
                        success: function (response) {
                            this.setState({ impattiComposite: response.data });
                        }.bind(this),
    
                        error: function (xhr, resp, text) {
                            // show error to console
                            console.error('Error', xhr, resp, text)
                            alert(xhr, resp, text);
                        }
                    });
            },
    
            render: function () {
        ...
        React.createElement('table', {style:null}, React.createElement('tbody', null,trElementList(this.state.impattiComposite, this),))
        ...
        }
    
    
    
                //my input text
                var inputTextarea = //end my input text
    
                        tdElement = React.createElement('td', { style: null }, inputTextarea);
                        tdList.push(tdElement);//add a component
    
            //./InputTextComponent.js
            import React from 'react';
    
            export class InputTextComponent extends React.Component {
              constructor(props) {
                super(props);
                this.state = {
                  idImpatto: props.idImpatto,
                  value: props.value,
                  noteType: props.noteType,
                  _impattiComposite: props.impattiComposite,
                };
                this.updateNote = this.updateNote.bind(this);
              }
    
            //Update a inpute text with new value insert of the user
    
              updateNote(event) {
                this.setState({ value: event.target.value });//update a state of the local componet inputText
                var impattiComposite = this.state._impattiComposite;
                var index = this.state.idImpatto - 1;
                var impatto = impattiComposite[index];
                impatto[this.state.noteType] = event.target.value;
                this.setState({ _impattiComposite: impattiComposite });//update of the state of the father component (tableComponet)
    
              }
    
              render() {
                return (
                  
                  
                );
              }
            }
    

提交回复
热议问题