React Js,我的输入在渲染时失去焦点

2023-12-15

嘿,我是 React js 新手。 任何人都可以帮助回答这个问题:如何防止输入字段在打字时失去焦点? 每次我在某个输入字段中输入内容时,该字段就会失去焦点。

注:第一个 <div className="container_info">输入工作得很好。

import React, { Fragment, useState } from "react";
import {
    FaRegSave
} from "react-icons/fa";
import axios from 'axios';
import { Tabs, Tab } from '@material-ui/core'
const InputEemp = (index) => {
    /* information publiques */
    const [fname, setFname] = useState("");
    const [lname, setLname] = useState("");
    const [gender, setGender] = useState("");
    const [tel_pro_portable, setTelPportable] = useState("");
    const [lieu_travail, setLieu_Travail] = useState("");
    const [adresse_elec_pro, setAdresseEpro] = useState("");
    const [tele_pro, setTelePro] = useState("");
    const [departement, setDepartement] = useState("");
    const [titre_post, setTitrePost] = useState("");
    const [gestionnaire, setGestionnaire] = useState("");
    const [mentor, setMentor] = useState("");
    const [temp_travail, setTempTravail] = useState("");
    const [autre, setAutre] = useState("");
    /* end of information publiques */
    //appbar************
    const [tabValue, setValue] = React.useState(0);
    const handlerTabs = (e, val) => {
        console.warn(val);
        setValue(val);
    }
    function TabPanel(props) {
        const { children, value, index } = props;
        return (
            <div>
                {
                    value === index && (<div>{children}</div>
                    )
                }
            </div>
        )
    }


    const addEmpll = async e => {
        e.preventDefault();
        try {
            await axios.post('http://localhost:5000/employees', {
                f_name: fname,
                l_name: lname,
                gender: gender,
                tel_pro_portable: tel_pro_portable,
                lieu_travail: lieu_travail,
                adresse_elec_pro: adresse_elec_pro,
                tele_pro: tele_pro,
                departement: departement,
                titre_post: titre_post,
                gestionnaire: gestionnaire,
                mentor: mentor,
                temp_travail: temp_travail,
                autre: autre
            }, {
                headers: {
                    'Content-Type': 'application/json'
                }
            })/* .then(res => {
                    console.log(res.data);
                }); */
            window.location = ('/');
        } catch (err) {
            console.log(err.message);
        }
    };
    return (
        <Fragment>
            <div className="container_emp">
                <form onSubmit={addEmpll}>
                    <div className="container_info">
                        <div className="header">Profile de l'Employe : <button type="submit" className="sauver_btn btn btn-success"><FaRegSave />&nbsp;&nbsp;Sauver</button>
                            <hr className="hr_padding" />
                        </div>
                        <div className="side_content imgCenter"><img src=".\nouser.png" alt="emp" className="rounded-circle bg-secondary" style={{ "width": "120px" }} /></div>
                        <div className="content">
                            <div className="content_cc">

                                <div className="nom_label"><label className="font-weight-bold text-secondary col">Nom :</label></div>
                                <div className="nom_input"><input type="text" className="form-control col" placeholder="insérez votre nom" value={fname} onChange={e => setFname(e.target.value)} /></div>

                                <div className="prenom_label"><label className="font-weight-bold text-secondary col">Prénom :</label></div>
                                <div className="prenom_input"><input type="text" className="form-control col" placeholder="insérez votre Prénom" value={lname} onChange={e => setLname(e.target.value)} /></div>

                                <div className="sexe_label"><label className="font-weight-bold text-secondary col">Sexe :</label></div>
                                <div className="F_radio">
                                    <label className="radio-inline pr-5">
                                        <input type="radio" name="gender" value="Masculin" onChange={e => setGender(e.target.value)} /> Masculin
                                    </label>
                                </div>
                                <div className="M_radio">
                                    <label className="radio-inline">
                                        <input type="radio" name="gender" value="Féminin" onChange={e => setGender(e.target.value)} /> Féminin
                                    </label>
                                </div>

                            </div>
                        </div>
                        <div className="footer imgCenter"> <input type="file" className="form-control-file" id="exampleFormControlFile1" text="Changer l'image"></input></div>
                    </div>

                    <div className="emp_info1">
                        <hr className="hr_padding" />
                        {/*  <AppBar position="static"> */}
                        <Tabs value={tabValue} onChange={handlerTabs}>
                            <Tab className="bg-dark text-white" label="Information Publiques" />
                            <Tab className="bg-dark text-white" label="Information Personnelle" />
                            <Tab className="bg-dark text-white" label="Paramètre RH" />
                            <Tab className="bg-dark text-white" label="Configuration de la paie" />
                            <Tab className="bg-dark text-white" label="Prêt logement" />
                        </Tabs>
                        {/*  </AppBar> */}
                        <TabPanel value={tabValue} index={0}>
                            <div className="info_publique">
                                <div className="info_contact">
                                    <div className="info_contact_child">
                                        <div className="info_contact_label text-info font-weight-bold"><h4>Information de contact</h4></div>
                                        <div className="tele_por">Tél. portable professionnel :</div>
                                        <div className="telePor_input"><input type="text" className="form-control col" placeholder="insérez votre Tél. portable professionnel" value={tel_pro_portable} onChange={e => setTelPportable(e.target.value)} /></div>
                                        <div className="lieu_tr">Lieu de Travail :</div>
                                        <div className="lieu_input"><input type="text" value={lieu_travail} onChange={e => setLieu_Travail(e.target.value)} className="form-control col" placeholder="insérez votre Lieu de Travail" /></div>
                                        <div className="adresse">Adresse électronique professionnel :</div>
                                        <div className="adrees_input"><input type="text" value={adresse_elec_pro} onChange={e => setAdresseEpro(e.target.value)} className="form-control col" placeholder="insérez votre Adresse électronique professionnel" /></div>
                                        <div className="tele_prof">Télephone professionnel :</div>
                                        <div className="tele_pro_input"><input type="text" value={tele_pro} onChange={e => setTelePro(e.target.value)} className="form-control col" placeholder="insérez votre Télephone professionnel" /></div>
                                    </div>
                                </div>
                                <div className="info_post">
                                    <div className="info_post_child">
                                        <div className="info_post_label text-info font-weight-bold"><h4>Post</h4></div>
                                        <div className="dep_label">Département :</div>
                                        <div className="dep_input">
                                            <select className="form-control" value={departement} onChange={e => setDepartement(e.target.value)}>
                                                <option value={0}>Select Département</option>
                                                <option>IT</option>
                                                <option>dep 1</option>
                                                <option>dep 2</option>
                                            </select>
                                        </div>
                                        <div className="titre_label">Titre du poste :</div>
                                        <div className="titre_input">
                                            <select className="form-control" value={titre_post} onChange={e => setTitrePost(e.target.value)}>
                                                <option value={0}>Select Titre poste</option>
                                                <option>poste 1</option>
                                                <option>poste 2</option>
                                            </select>
                                        </div>
                                        <div className="gestionnaire_label">Gestionnaire :</div>
                                        <div className="gestionnaire_input">
                                            <select className="form-control" value={gestionnaire} onChange={e => setGestionnaire(e.target.value)}>
                                                <option value={0}>Select Gestionnaire</option>
                                                <option>Gestionnaire 1</option>
                                                <option>Gestionnaire 2</option>
                                            </select>
                                        </div>
                                        <div className="mentor_label">Mentor :</div>
                                        <div className="mentor_input">
                                            <select className="form-control" value={mentor} onChange={e => setMentor(e.target.value)}>
                                                <option value={0}>Select Mentor</option>
                                                <option>Mentor 1</option>
                                                <option>Mentor 2</option>
                                            </select>
                                        </div>
                                        <div className="T_travail_label">Temps de travail :</div>
                                        <div className="T_travail__input">
                                            <select className="form-control" value={temp_travail} onChange={e => setTempTravail(e.target.value)}>
                                                <option value={0}>Select Temps travail</option>
                                                <option>Temps travail 1</option>
                                                <option>Temps travail 2</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div className="autre_info">
                                    <textarea className="form-control" rows="2" placeholder="Autre information" value={autre} onChange={e => setAutre(e.target.value)}></textarea>
                                </div>
                            </div>
                        </TabPanel>
                        <TabPanel value={tabValue} index={1}>this is : Information Personnelle</TabPanel>
                        <TabPanel value={tabValue} index={2}>this is : Paramètre RH</TabPanel>
                        <TabPanel value={tabValue} index={3}>this is : Configuration de la paie</TabPanel>
                        <TabPanel value={tabValue} index={4}>this is : Prêt logement</TabPanel>

                    </div>

                </form>
            </div>
        </Fragment>
    )
};

export default InputEemp;

Issue

The TabPanel组件在父级内部声明InputEemp,因此每个渲染周期都会重新创建它。换句话说,每个渲染周期TabPanel is a new反应组件;老人TabPanel组件已卸载,新的TabPanel组件已安装,从而失去了旧组件的焦点。

它通常被认为是在其他 React 组件中定义 React 组件的 React 反模式。

Solution

移动TabPanel组件声明outside the InputEemp成分。

Example:

function TabPanel(props) {
  const { children, value, index } = props;
  return <div>{value === index && <div>{children}</div>}</div>;
}

const InputEemp = (index) => {
  /* information publiques */
  ...
  /* end of information publiques */

  ...

  return ( ... );
};

Edit react-js-my-input-losing-focus-when-rendering

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React Js,我的输入在渲染时失去焦点 的相关文章

随机推荐

  • 如何防止用户输入零作为输入?

    我正在尝试将输入验证添加到一组三个EditTexts 用户在其中输入数值 我面临的问题是为了计算工作 用户不能输入零 作为输入字段之一 否则应用程序将崩溃 我尝试执行以下操作以防止输入零并显示警告消息 我收到一条错误消息 指出 无法启动活动
  • 从 Shadertoy 导出到 Three.js

    我正在迈出编码的第一步 我在互联网上制作了一些课程 然后进行了一些 Three js 实验 现在我想继续学习着色器实验 我找到了 Shadertoy com 它真的太棒了 有很多不同的实验 效果令人难以置信 我正在尝试在 Three js
  • 无法使用partitionByInstrument将文件分成多个部分

    使用music21无法将文件分成使用partitionByInstrument from music21 import song converter parse Nottingham train ashover simple chords
  • 验证长度 Powershell

    这是我的第一个剧本 所以不要打我 我正在编写一个脚本 该脚本根据用户输入创建网络目录和 AD 组 以下是我到目前为止所得到的 它有效 但我想进行一些改进 我想验证用户输入的长度 我找到了一篇文章 使用读取主机的 PowerShell Val
  • 无需凭据即可获取不同用户的特殊文件夹

    我正在编写一个卸载程序 作为该过程的一部分 我想为所有本地用户清理缓存 临时文件等 该应用程序将运行提升以使其正常工作 我正在查找的文件位于特殊文件夹中 例如AppData Local 所以我需要路径 对于当前登录的用户来说 这是微不足道的
  • React 中延迟数组映射迭代

    我有这个数组 我想迭代它 我需要在下一个之前延迟几秒钟 this props things map thing index gt return div thing content div Delay 1 second here 该数组的初始
  • 项目匹配查询不存在错误?

    这是视图 def showProject request project slug project Project objects get slug project slug tickets Ticket objects filter pr
  • 为什么 autoconf 会错误地找到一个以后不可用的函数?

    在 Linux 系统上 我配置了一个软件包 llvm autoconf 找到了arc4random功能 这里是配置期间输出的提取 checking for strerror yes checking for strerror r yes c
  • 如何以 JSON 形式发送 POST 请求?

    data ids 12 3 4 5 6 urllib2 urlopen http abc example api posts create urllib urlencode data 我想发送 POST 请求 但其中一个字段应该是数字列表
  • Swift 只读外部,可读写内部属性

    在 Swift 中 定义通用模式的传统方法是什么 其中属性是外部只读的 但可以由拥有它的类 和子类 在内部修改 在 Objective C 中 有以下选项 在接口中将属性声明为只读 并使用类扩展在内部访问该属性 这是基于消息的访问 因此它可
  • 一个衬垫:从列表创建一个字典,以索引为键

    我想根据给定的列表创建一本字典 仅一行 字典的键将是索引 值将是列表的元素 像这样的事情 a 51 27 13 56 given list d one line statement one line statement to create
  • 以简单的方式导出到 Excel(但不是 CSV)

    昨天我发现了这个How To通过简单地生成 HTML 将数据导出到 Excel 电子表格 这似乎是一种直接导出带格式的单元格的方法 而仅使用 CSV 格式是无法实现的 我的想法是在Java应用程序中处理一些数据 将结果导出到Excel电子表
  • 为什么 JSLint 不允许在 for 循环中使用“var”?

    我的代码或 plovr 有问题 我去 JSLint 寻求帮助 然而 JSLint 似乎认为这是一个致命错误 并拒绝检查更多代码 for var i 0 i lt data length i 4 为什么 我喜欢这种宣告 我 的方式 如果您使用
  • 跟踪对象的“页面浏览量”或“点击量”数量?

    我确信有人有一个可插拔的应用程序 或教程 可以近似于此 但我很难找到它 我希望能够跟踪特定对象的 视图 数量 就像这里的问题一样 stackoverflow 上有一个 观看次数 如果用户没有登录 我不介意尝试放置 cookie 或记录 IP
  • Ant 使用了错误的 java 版本

    我正在使用 Ant 1 7 0 并安装了位于 JAVA HOME 中的 java 1 6 我想使用 java 1 5 构建一个项目 因此我已将 JAVA HOME 导出为我的 java 1 5 目录 java version 说 1 5 当
  • 为什么 CUDA 的示例 makefile 找不到 CUDA 库?

    我正在运行 Arch Linux 并已从存储库安装了 cuda sdk 和 cuda toolkit 我已经编译了 opt cuda sdk CUDALibraries 中的库 不 我通过在 opt cuda sdk C 中运行 make
  • 使用 Chapel 处理海量矩阵

    我最近偶然发现了 Chapel 我非常想尝试一下 我有一个双重问题 希望它能解决 我通常使用 Python 或 C 工作 当Java陷入困境时 我有两个矩阵I and V 两者都很稀疏 尺寸约为 600K x 600K 密度约为 1 首先
  • 在高清图像上应用 WebGL 滤镜会剪切图像 (fabric V2-beta-6)

    目前正在与面料2 0 6对高清图像应用滤镜 我需要在最短的时间内应用过滤器 为此 我使用 webGL 方法来应用过滤器 如下所述这里 fabric js 演示 如果您将其应用于大小 Original Image size 1 90 MB H
  • 为什么这段代码中图像下方有空格? [复制]

    这个问题在这里已经有答案了 我有以下代码 它允许红色从 a 元素中显示出来 为什么是这样 我本以为 a 元素只会扩展到内容的大小 但看起来它比这个大一点 请参阅此处的代码笔http codepen io anon pen soqEz HTM
  • React Js,我的输入在渲染时失去焦点

    嘿 我是 React js 新手 任何人都可以帮助回答这个问题 如何防止输入字段在打字时失去焦点 每次我在某个输入字段中输入内容时 该字段就会失去焦点 注 第一个 div 输入工作得很好 import React Fragment useS