输入正在失去对钩子更新的关注 [英] Input is loosing focus on hooks update

查看:26
本文介绍了输入正在失去对钩子更新的关注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 reactjs 的新手,我正在尝试从输入中读取数据.问题是当我输入一个符号时,我的输入会失去焦点.但只有当所有逻辑都在函数内部时.当带有按钮和逻辑的输入位于不同的文件中时 - 它正在工作.我真的不知道为什么...

我用相同的代码创建了单独的文件并将其导入到 sollution - 没关系.我试过 onChange={handleChange} - 也失去了焦点.

导出默认函数 MyInput(){const [citySearch, updateCitySearch] = useState();函数 searchCityClick(){警报(城市搜索);}const SearchComponent = ()=>(<div><输入值={citySearch}onChange={(e) =>updateCitySearch(e.target.value)}/><Button variant="contained" color="primary" onClick={searchCityClick}>搜索</按钮>

);返回(<div><div><搜索组件/>

)}

解决方案

SearchComponent 是一个功能组件,不应在另一个组件中定义.在 MyInput 中定义 SearchComponent 将导致 SearchComponent 被重新创建(不重新渲染),本质上它的 DOM 将被删除,然后重新添加每次点击.

解决方案非常简单,从 MyInput 中提取 SearchComponent,然后通过 props 对象:

const { useState, useCallback } = React;const SearchComponent = ({ citySearch, updateCitySearch, searchCityClick }) =>(<div><输入值={citySearch}onChange={e =>updateCitySearch(e.target.value)}/><button onClick={searchCityClick}>搜索</button>

);const MyInput = () =>{const [citySearch, updateCitySearch] = useState('');const searchCityClick = () =>警报(城市搜索);返回(<div><搜索组件城市搜索={城市搜索}updateCitySearch={updateCitySearch}searchCityClick={searchCityClick}/>

);};ReactDOM.render(<我的输入/>,根);

<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script><div id="root"></div>

I'm new to reactjs and I'm trying read data from input. Problem is when I type a sign, my input loose focus. But only when all logic is inside function. When Input with button and logic is in different file - it's working. I don't really know why...

I have created separate file with same code and import it to sollution - it's ok. I have tried with onChange={handleChange} - lost focus as well.

export default function MyInput(){
const [citySearch, updateCitySearch] = useState();

function searchCityClick(){
   alert(citySearch);
}
const SearchComponent = ()=> (
    <div> 
        <input 
        value={citySearch}
        onChange={(e) => updateCitySearch(e.target.value)}/>
        <Button variant="contained" color="primary" onClick={searchCityClick}>
            Search
        </Button>
    </div>

);
return(
    <div>
        <div>
            <SearchComponent />
        </div>
    </div>
)}

解决方案

The SearchComponent is a functional component, and shouldn't be defined inside another component. Defining SearchComponent inside MyInput will cause SearchComponent to be recreated (not rerendered), and in essence it's DOM would be removed, and then added back on every click.

The solution is pretty straightforward, extract SearchComponent from MyInput, and pass the functions, and the data via the props object:

const { useState, useCallback } = React;

const SearchComponent = ({ citySearch, updateCitySearch, searchCityClick }) => (
  <div> 
    <input
      value={citySearch}
      onChange={e => updateCitySearch(e.target.value)} />
    
    <button onClick={searchCityClick}>Search</button>
  </div>
);

const MyInput = () => {
  const [citySearch, updateCitySearch] = useState('');

  const searchCityClick = () => alert(citySearch);

  return(
    <div>
      <SearchComponent 
        citySearch={citySearch} 
        updateCitySearch={updateCitySearch}
        searchCityClick={searchCityClick} />
    </div>
  );
};

ReactDOM.render(
  <MyInput />,
  root
);

<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>

<div id="root"></div>

这篇关于输入正在失去对钩子更新的关注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆