Autodesk / Autodesk/react-base-table

selections not working if columns pulled from state?

オープン
#418 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
TypeScript
スター
1.5k
フォーク
170
PR マージ指標
30日以内にマージされた PR はありません

説明

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 }) => (
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 (


);
};

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 }) => (
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 (


);
};

export default MultiSelectTable;

```

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

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

提供された MultiSelectTable の例を再現し、useEffect で作成された columns と BaseTable に渡されるインラインの columns 配列を比較します。状態に基づく columns と cellRenderer が selectedRows をどのように監視するかを追跡し、チェックボックスを選択すると selectedRows とレンダリングされたチェックボックスの状態の両方が更新されることを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, react
領域
frontend
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
30/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。