英文:
Trying to pass data to a parent component from ReactDataGrid
问题
我正在尝试从表格组件(我正在使用ReactDataGrid库来渲染表格)中传递一个id到父组件,以便我可以在不同的子组件中使用它。
这是我的父组件:
const CallForm = (props) => {
const [isNewOpen, setIsNewOpen] = useState(false);
const toggleNewPopup = () => {
setIsNewOpen(!isNewOpen);
}
const [isEditOpen, setIsEditOpen] = useState(false);
const toggleEditPopup = () => {
setIsEditOpen(!isEditOpen);
}
return (
<Table type={props.screenType === "taskScreen" ? "tasks" : "calls"} />{/* 试图从这里接收id */}
<div className="actions">
<div className="buttons-actions">
<button onClick={toggleNewPopup}>{newCallText}</button>
{isNewOpen && <CallPopUp
state="add"
screenType={props.screenType}
handleClose={toggleNewPopup}
/>}
<button onClick={toggleEditPopup}>{editCallText}</button>
{isEditOpen && <CallPopUp
state="edit"
id={/* 在这里传递id */}
screenType={props.screenType}
handleClose={toggleEditPopup}
/>}
<button onClick={props.screenType === "taskScreen" ? closeTask : closeCall}> {closeCallText}</button>
<button onClick={props.screenType === "taskScreen" ? deleteTask : deleteCall}>{deleteCallText}</button>
<button onClick={props.screenType === "taskScreen" ? moveTask : moveCall}>{moveCallText}</button>
</div>
<div className="free-search">
<label htmlFor="freeSearch">Free Search</label>
<input type="text" name='freeSearch' id='freeSearch' />
</div>
</div>
}
这是Table组件:
const Table = (props) => {
const [elementId, setElementId] = useState("");
return (
<div>
<ReactDataGrid
idProperty='id'
columns={headers}
dataSource={allTasks}
theme='default-light'
rtl={true}
scrollThreshold={false}
enableColumnAutosize={true}
{/* 将id返回给父组件 */}
/>
<div className='info'>{rowsText}</div>
</div>
)
}
这是CallPopUp组件:
const CallPopUp = (props) => {
return (
{/* 包括元素的输入,如果状态是“edit”,则在值中显示旧数据 */}
{/* 为了从服务器调用API的get、put或delete请求,我需要来自表格元素的id */}
)
}
我尝试使用https://reactdatagrid.io/docs/
的文档,并找到了一些我认为可能有帮助的解决方案,但我不明白如何使用它们。
例如:
源:https://reactdatagrid.io/docs/api-reference#method-getItemAt
getItemAt
Fn(index)
Number
返回dataSource中指定索引的项目。
源:https://reactdatagrid.io/docs/api-reference#method-getRowId
getRowId
Fn(index)
String|Number
返回指定索引处行的id。
源:https://reactdatagrid.io/docs/row-selection
单行选择和键盘导航
让我们从一个不支持选择的简单示例开始。尝试点击下面的<ReactDataGrid />,然后使用上/下键盘箭头导航。请注意,当您按向上/向下箭头键时,当前活动的行会更改。默认情况下,启用了键盘导航,但您可以通过指定enableKeyboardNavigation=false来关闭它。您还可以使用home/end或page up/down来导航<ReactDataGrid />行。此外,当您将鼠标移动到<ReactDataGrid />行上时,它会显示悬停状态(可以使用showHoverRows=false来禁用此功能)。
使用activeIndex(受控)或defaultActiveIndex(不受控),结合onActiveIndexChange,可以完全控制用户导航和<ReactDataGrid />中当前活动的行。
当使用受控的activeIndex属性和onActiveIndexChange回调属性来更新activeIndex时,如果在渲染函数上执行其他计算密集型操作,您可能会注意到在用户保持向上/向下箭头键按下以快速导航到其他记录的情况下性能下降。如果是这种情况,最好使用不受控的defaultActiveIndex属性来获得更好的性能。
在使用键盘导航导航<ReactDataGrid />时,由于活动索引的更新,<ReactDataGrid />会自动滚动以在视口中显示当前活动的行。
英文:
I am trying to pass an id from a table component (I am using the ReactDataGrid library to render a table) to the parent component, so that I can use it in a different child component.
Here's my parent component:
const CallForm = (props) => {
const [isNewOpen, setIsNewOpen] = useState(false);
const toggleNewPopup = () => {
setIsNewOpen(!isNewOpen);
}
const [isEditOpen, setIsEditOpen] = useState(false);
const toggleEditPopup = () => {
setIsEditOpen(!isEditOpen);
}
return (
<Table type={props.screenType === "taskScreen" ? "tasks" : "calls"} />//Trying to receive the id from here.//
<div className="actions">
<div className="buttons-actions">
<button onClick={toggleNewPopup}>{newCallText}</button>
{isNewOpen && <CallPopUp
state="add"
screenType={props.screenType}
handleClose={toggleNewPopup}
/>}
<button onClick={toggleEditPopup}>{editCallText}</button>
{isEditOpen && <CallPopUp
state="edit"
id={}//Pass the id in here.//
screenType={props.screenType}
handleClose={toggleEditPopup}
/>}
<button onClick={props.screenType === "taskScreen" ? closeTask : closeCall}> {closeCallText}</button>
<button onClick={props.screenType === "taskScreen" ? deleteTask : deleteCall}>{deleteCallText}</button>
<button onClick={props.screenType === "taskScreen" ? moveTask : moveCall}>{moveCallText}</button>
</div>
<div className="free-search">
<label htmlFor="freeSearch">Free Search</label>
<input type="text" name='freeSearch' id='freeSearch' />
</div>
</div>
}
This is the Table component:
const Table = (props) => {
const [elementId, setElementId] = useState("");
return (
<div>
<ReactDataGrid
idProperty='id'
columns={headers}
dataSource={allTasks}
theme='default-light'
rtl={true}
scrollThreshold={false}
enableColumnAutosize={true}
//Return the id to parent component.//
/>
<div className='info'>{rowsText}</div>
</div>
)
This is the CallPopUp component:
const CallPopUp = (props) => {
return (
//Inputs including elements, in case of "edit" state the old data is shown in values.//
//In order for me to call to the API get, put or delete request from server I need the id from the table element.//
I tried to use the docs from https://reactdatagrid.io/docs/
and I ran into some solutions I thought could help, but I didn't understand how I can use them.
for example:
The source: https://reactdatagrid.io/docs/api-reference#method-getItemAt
getItemAt
Fn(index)
Number
Returns the item at the specified index in the dataSource.
The source: https://reactdatagrid.io/docs/api-reference#method-getRowId
getRowId
Fn(index)
String|Number
Returns the id of the row at the specified index.
The source: https://reactdatagrid.io/docs/row-selection
Single row selection and keyboard navigation
Let's start with a simple example without support for selection. Try clicking the <ReactDataGrid /> below and then navigate with up/down keyboard arrows. Notice the currently active row changes as you hit arrow up/down. By default keyboard navigation is enabled, but you can turn it off by specifying enableKeyboardNavigation=false. You can also use home/end or page up/down to navigate the <ReactDataGrid /> rows. Additionally, when you move your mouse over a <ReactDataGrid /> row, it shows a hover state (this can be disabled by using showHoverRows=false).
Use activeIndex (controlled) or defaultActiveIndex (uncontrolled), combined with onActiveIndexChange to have full control on user navigation and the currently active row in the <ReactDataGrid />.
When using the controlled activeIndex prop and onActiveIndexChange callback prop to update the activeIndex, if you're doing other compute intensive stuff on the render function, you might notice some performance degradation in the scenario when the user keeps arrow up/down key pressed to quickly navigate to other records. If this is the case, it's better to use the uncontrolled defaultActiveIndex prop instead to obtain better performance.
When navigating the <ReactDataGrid /> with keyboard navigation, as the active index is updated, the <ReactDataGrid /> is automatically scrolled to show the currently active row in the viewport.
答案1
得分: 1
在你的父组件中,定义一个useState来保存id:
const [elementId, setElementId] = useState("");
将setElementId
传递给子组件,方式如下(以便子组件可以更新elementId
):
<Table type={props.screenType === "taskScreen" ? "tasks" : "calls"} setElementId={setElementId} />
在你的子组件中,添加一个onSelectionChange
方法,在方法中调用props.setElementId(data.id);
:
const Table = (props) => {
const onSelectionChange = useCallback(({ data }) => {
props.setElementId(data.id);
}, []);
return (
<div>
<ReactDataGrid
idProperty="id"
columns={headers}
dataSource={allTasks}
theme="default-light"
rtl={true}
scrollThreshold={false}
enableColumnAutosize={true}
onRowChange={onSelectionChange} // 不要忘记附加它
/>
</div>
);
};
英文:
In your parent, define a useState to hold the id:
const [elementId, setElementId] = useState("");
Pass down the setElementId
to the child like so (so the child can update the elementId
):
<Table type={props.screenType === "taskScreen" ? "tasks" : "calls"} setElementId={setElementId />
In your child component, add a onSelectionChange
and in the method, call props.SetElementId(data.id);
:
const Table = (props) => {
const onSelectionChange = useCallback(({data}) => {
props.setElementId(data.id);
}, [])
return (
<div>
<ReactDataGrid
idProperty="id"
columns={headers}
dataSource={allTasks}
theme="default-light"
rtl={true}
scrollThreshold={false}
enableColumnAutosize={true}
onRowChange={onSelectionChange} // don't forget to attach it
/>
</div>
);
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论