Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

selections not working if columns pulled from state?

Abierto
#418 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
30/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
javascript, react
Área
frontend

Línea de trabajo

Reproducir el ejemplo proporcionado de MultiSelectTable, comparando las columns creadas en useEffect con el array de columns inline pasado a BaseTable. Seguir cómo las columns respaldadas por el estado y cellRenderer observan selectedRows y, después, verificar que seleccionar una casilla de verificación actualiza tanto selectedRows como el estado renderizado de la casilla de verificación.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

In my application, I need to dynamically create the columns and then have the pull the columns from the state. However, when I do this, the code to update the row state does not work. Below is a simplified version, showing the issue. If you run this code, when you select the checkbox, the state does not get updated correctly.

If, however, I change BaseTable and set the columns= the actual array, then the selection process works.

Code that does not work

/* eslint-disable */
import React, { useState,useEffect } from 'react';
import BaseTable, { Column } from 'react-base-table';
import 'react-base-table/styles.css';

const MultiSelectTable = () => {
  const [selectedRows, setSelectedRows] = useState([]);
  const [columns, setColumns] = useState([])


  useEffect(()=>{
    setColumns(
      [
        {
          key:"checkbox",
          title:"",
          width:50,
          selectedRowKeys:{selectedRows},
          cellRenderer:({ rowData }) => (
                <input
                  type="checkbox"
                  checked={selectedRows.includes(rowData.id)}
                  onChange={() => handleRowSelection({ rowData })}
                  />
            )}
          ,
        {key:"name", dataKey:"name", title:"Name", width:200},
        {key:"age", dataKey:"age", title:"Age", width:100}
        ]
    )
  },[])

  const handleRowSelection = ({ rowData }) => {
    const selectedRowKeys = [...selectedRows];
    const rowIndex = selectedRowKeys.indexOf(rowData.id);

    if (rowIndex !== -1) {
      selectedRowKeys.splice(rowIndex, 1);
    } else {
      selectedRowKeys.push(rowData.id);
    }

    setSelectedRows(selectedRowKeys);
    console.log(selectedRowKeys)
  };



  const data = [
    { id: 1, name: 'John Doe', age: 25 },
    { id: 2, name: 'Jane Smith', age: 30 },
    { id: 3, name: 'Bob Johnson', age: 35 },
    // Add more data as needed
  ];

  return (
    <BaseTable
      width={600}
      height={400}
      data={data}
      columns={columns}
      rowKey="id" 
    />
   
  );
};

export default MultiSelectTable;

Code that works:

/* eslint-disable */
import React, { useState,useEffect } from 'react';
import BaseTable, { Column } from 'react-base-table';
import 'react-base-table/styles.css';

const MultiSelectTable = () => {
  const [selectedRows, setSelectedRows] = useState([]);

  const handleRowSelection = ({ rowData }) => {
    const selectedRowKeys = [...selectedRows];
    const rowIndex = selectedRowKeys.indexOf(rowData.id);

    if (rowIndex !== -1) {
      selectedRowKeys.splice(rowIndex, 1);
    } else {
      selectedRowKeys.push(rowData.id);
    }

    setSelectedRows(selectedRowKeys);
    console.log(selectedRowKeys)
  };

  const columns=  [
    {
      key:"checkbox",
      title:"",
      width:50,
      selectedRowKeys:{selectedRows},
      cellRenderer:({ rowData }) => (
            <input
              type="checkbox"
              checked={selectedRows.includes(rowData.id)}
              onChange={() => handleRowSelection({ rowData })}
              />
        )}
      ,
    {key:"name", dataKey:"name", title:"Name", width:200},
    {key:"age", dataKey:"age", title:"Age", width:100}
    ]


  const data = [
    { id: 1, name: 'John Doe', age: 25 },
    { id: 2, name: 'Jane Smith', age: 30 },
    { id: 3, name: 'Bob Johnson', age: 35 },
    // Add more data as needed
  ];

  return (
    <BaseTable
      width={600}
      height={400}
      data={data}
      columns={columns}
      rowKey="id" 
    />
   
  );
};

export default MultiSelectTable;

How can I load the columsn dynamically into state but still have the selection of rows work?

Lenguaje dominante
TypeScript
Estrellas
1.5k
Forks
170
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Autodesk/react-base-table

Todos los issues de Autodesk/react-base-table

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.