简体   繁体   English

在 onChange 事件后反应输入元素重新呈现

[英]React input element re-renders after onChange event

I have a input element that is changed when the user inputs some value:我有一个输入元素,当用户输入一些值时它会发生变化:

const[elem, setElem]=useState("");

const handleChangeElem = (event) => {
    setElem(event.value);
...
 <Input onChange={handleChangeElem}/>
}

So, when user writes, for examle, 'test1' the elem state should be 'test1'.因此,当用户写入例如“test1”时,elem state 应该是“test1”。 But the thing is, after I write 1 letter, the whole component re-renders, and the input is empty.但问题是,在我写完 1 个字母后,整个组件重新渲染,输入为空。 The elem state is not mentioned anywhere else (useeffect or smth like that) so I don't know why it keeps re-rendering. elem state 没有在其他任何地方提到(useeffect 或类似的东西)所以我不知道为什么它不断重新渲染。 Help anyone?帮助任何人?

my whole component:我的整个组成部分:

import { useTheme } from '@mui/material/styles';
import { process } from "@progress/kendo-data-query";
import { Button } from '@progress/kendo-react-buttons';
import { Grid, GridColumn } from "@progress/kendo-react-grid";
import '@progress/kendo-theme-default/dist/all.css';
import React, { useEffect, useState } from 'react';
import { GiJumpAcross } from 'react-icons/gi';
import { BiRefresh } from 'react-icons/bi';
import { useNavigate } from "react-router-dom";
import services from '../../services';
import { FaTrash, FaDownload, FaUpload } from 'react-icons/fa';
import { FcCancel } from 'react-icons/fc';
import { Dialog, DialogActionsBar } from "@progress/kendo-react-dialogs";
import { Input } from '@progress/kendo-react-inputs';
import { GiCancel } from 'react-icons/gi';
import { AiFillFileAdd } from 'react-icons/ai';
import { useDispatch, useSelector } from "react-redux";

export default  function Sesije ({state}) {
  const toggleDialog = () => {
      setVisible(!visible);
    };

  const toggleDialogPrilog = () => {
    setVisible2(!visible2);
  }


  const storno = (nServisnaId, vKorisnikId, vStornoSustav) => {
    (async() => {
      const resp = await services.stornoSession(nServisnaId, vKorisnikId, napomena,vStornoSustav,razlog);
      alert(resp.data.vporuka);
    })();
}

const handleChangeRazlog = (event) => {
    setRazlog(event.value);
    console.log(event);
}

const handleChangeNapomena = (event) => {
    setNapomena(event.value);
}

  const MyCommandCell = (props) => {
      const {
        dataItem
      } = props;

      return <td className="k-command-cell">
        <div style={{marginTop:'2%'}}>
          
          <Button style={{width:'8vw',marginTop:'2%'}}
          onClick={toggleDialog}>
           <FcCancel/> Storniraj
          </Button>
          </div>
          { visible && <Dialog onClose={toggleDialog} title={"Storniranje sesije"}>
            <DialogActionsBar>
            <div style={{marginTop:'2%'}}>Razlog storna:</div>
            <Input name="razlog" onChange={handleChangeRazlog} style={{marginTop:'2%'}}/>
            <div style={{marginTop:'2%'}}>Napomena:</div>
            <Input value={bzvz} onChange={handleChangeNapomena} style={{marginTop:'2%'}}/>
            <Button 
            onClick={toggleDialog}>
            <GiCancel/> Odustani
            </Button>
            <Button 
            onClick={()=>storno(props.dataItem.sn_zag_id, currentUser.userName, stornoSus)}>
            <FaTrash/> Izbriši servisnu
            </Button>
            </DialogActionsBar>
          </Dialog>}
       
          </td>;
  const[elem, setElem]=useState("");
        
        const handleChangeElem = (event) => {
            setElem(event.value);
        ...
  }
  <Input value={elem} onChange={(e)=>handleChangeElem(e)}/>
        

when you trigger a state update you re-render the view.当您触发 state 更新时,您会重新呈现视图。 if the component is not depending on your state it will re-render in its vanilla state.如果该组件不依赖于您的 state,它将在其原版 state 中重新呈现。

supplying value={elem} to your Input should fix your problem向您的 Input 提供value={elem}应该可以解决您的问题

replace your input tag with this用这个替换你的输入标签

<Input value={elem} onChange={handleChangeElem}/>

you can resolve this by just adding a value attribute to your tag.您只需在标签中添加一个值属性即可解决此问题。

You can use the useRef hook to perform the work more efficiently您可以使用useRef挂钩更有效地执行工作

const elem=useRef("");
 <Input ref={elem}/>
}

Then whenever you type anything in the text box, the sentence will be assigned to elem variable.然后,每当您在文本框中键入任何内容时,该句子将被分配给elem变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM