Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
How to add focus using dynamic useRef on td on edit button click after setting contentEditable True

I am creating here dynamic table, and I am trying to edit content of td on click of edit button click, so I am setting contentEditable true to each td on click of respective edit button, now i want to add focus by using useRed, but dynamic focus is not working, I refer other problems also on stackover flow, but nothing worked.

import react from "react";
import { useEffect, useState, useRef } from "react";

export const Table = (props) => {
    const [contentEditableMode, setContentEditableMode] = useState(false)
    const [contentEditableId, setContentEditableId] = useState(null);
    const editTd = (e, id) => {
        console.log(e.target);
        console.log(id);
        setContentEditableMode(!contentEditableMode);
        setContentEditableId(id);
    }
    useEffect(() => {

    }, [contentEditableMode])
    
    
    return (<div><table>
        <thead>
            <tr>
                <td>ID</td>
                <td>Title</td>
                <td style={{ "paddingRight": "20px" }}>Edit</td>
                <td>Delete</td>
            </tr>
        </thead>
        <tbody>

            {props.tableData && props.tableData.map((row, ind) => {
                return (
                    <tr key={row.id}>
                        <td>{row.id}</td>
                        <td contentEditable={contentEditableId === row.id ? contentEditableMode : null} suppressContentEditableWarning='true'>{row.title}</td>
                        <td style={{ "paddingRight": "20px", "cursor": "pointer" }} onClick={(e) => { editTd(e, row.id) }}>Edit</td>
                        <td style={{ "cursor": "pointer" }} onClick={() => props.deleteRowData(row.id)}>Delete</td>

                    </tr>)
            })}
        </tbody>
    </table>

    </div>)

}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here I give you just one example, how you can use useRef to focus on the edit queue, if you want useRef in map you have to be unique in the ref values must be added and when you click on the edit line the status will be checked to see if the id exists in focusRef then you have to check the console and check if the focus attribute exists, If you have a focus attribute, you can add focus to the edit line. Thanks

import React, {useRef} from "react";
import { useEffect, useState, useRef } from "react";

export const Table = (props) => {
    const focusRef = useRef([]);

    const [contentEditableMode, setContentEditableMode] = useState(false)
    const [contentEditableId, setContentEditableId] = useState(null);
    const editTd = (e, id) => {
        if (focusRef.current && focusRef.current[id]) {
          console.log(focusRef.current[id])
          //focusRef.current[id].focus();
        }
        console.log(e.target);
        console.log(id);
        setContentEditableMode(!contentEditableMode);
        setContentEditableId(id);
    }
    useEffect(() => {

    }, [contentEditableMode])
    
    
    return (<div><table>
        <thead>
            <tr>
                <td>ID</td>
                <td>Title</td>
                <td style={{ "paddingRight": "20px" }}>Edit</td>
                <td>Delete</td>
            </tr>
        </thead>
        <tbody>

            {props.tableData && props.tableData.map((row, ind) => {
                return (
                    <tr key={row.id}>
                        <td>{row.id}</td>
                        <td contentEditable={contentEditableId === row.id ? contentEditableMode : null} suppressContentEditableWarning='true'>{row.title}</td>
                        <td ref={ref => (focusRef.current[row.id] = ref)} style={{ "paddingRight": "20px", "cursor": "pointer" }} onClick={(e) => { editTd(e, row.id) }}>Edit</td>
                        <td style={{ "cursor": "pointer" }} onClick={() => props.deleteRowData(row.id)}>Delete</td>
                    </tr>)
            })}
        </tbody>
    </table>

    </div>)

}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda