尝试从ReactDataGrid传递数据给父组件。

huangapple go评论88阅读模式
英文:

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) =&gt; {
const [isNewOpen, setIsNewOpen] = useState(false);
const toggleNewPopup = () =&gt; {
setIsNewOpen(!isNewOpen);
}
const [isEditOpen, setIsEditOpen] = useState(false);
const toggleEditPopup = () =&gt; {
setIsEditOpen(!isEditOpen);
}
return (
&lt;Table type={props.screenType === &quot;taskScreen&quot; ? &quot;tasks&quot; : &quot;calls&quot;} /&gt;//Trying to receive the id from here.//
&lt;div className=&quot;actions&quot;&gt;
&lt;div className=&quot;buttons-actions&quot;&gt;
&lt;button onClick={toggleNewPopup}&gt;{newCallText}&lt;/button&gt;
{isNewOpen &amp;&amp; &lt;CallPopUp
state=&quot;add&quot;
screenType={props.screenType}
handleClose={toggleNewPopup}
/&gt;}
&lt;button onClick={toggleEditPopup}&gt;{editCallText}&lt;/button&gt;
{isEditOpen &amp;&amp; &lt;CallPopUp
state=&quot;edit&quot;
id={}//Pass the id in here.//
screenType={props.screenType}
handleClose={toggleEditPopup}
/&gt;}
&lt;button onClick={props.screenType === &quot;taskScreen&quot; ? closeTask : closeCall}&gt; {closeCallText}&lt;/button&gt;
&lt;button onClick={props.screenType === &quot;taskScreen&quot; ? deleteTask : deleteCall}&gt;{deleteCallText}&lt;/button&gt;
&lt;button onClick={props.screenType === &quot;taskScreen&quot; ? moveTask : moveCall}&gt;{moveCallText}&lt;/button&gt;
&lt;/div&gt;
&lt;div className=&quot;free-search&quot;&gt;
&lt;label htmlFor=&quot;freeSearch&quot;&gt;Free Search&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&#39;freeSearch&#39; id=&#39;freeSearch&#39; /&gt;
&lt;/div&gt;
&lt;/div&gt;
}

This is the Table component:

const Table = (props) =&gt; {
const [elementId, setElementId] = useState(&quot;&quot;);
return (
&lt;div&gt;
&lt;ReactDataGrid
idProperty=&#39;id&#39;
columns={headers}
dataSource={allTasks}
theme=&#39;default-light&#39;
rtl={true}
scrollThreshold={false}
enableColumnAutosize={true}
//Return the id to parent component.//
/&gt;
&lt;div className=&#39;info&#39;&gt;{rowsText}&lt;/div&gt;
&lt;/div&gt;
)

This is the CallPopUp component:

const CallPopUp = (props) =&gt; {
return (
//Inputs including elements, in case of &quot;edit&quot; 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&#39;s start with a simple example without support for selection. Try clicking the &lt;ReactDataGrid /&gt; 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 &lt;ReactDataGrid /&gt; rows. Additionally, when you move your mouse over a &lt;ReactDataGrid /&gt; 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 &lt;ReactDataGrid /&gt;.
When using the controlled activeIndex prop and onActiveIndexChange callback prop to update the activeIndex, if you&#39;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&#39;s better to use the uncontrolled defaultActiveIndex prop instead to obtain better performance.
When navigating the &lt;ReactDataGrid /&gt; with keyboard navigation, as the active index is updated, the &lt;ReactDataGrid /&gt; 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(&quot;&quot;);

Pass down the setElementId to the child like so (so the child can update the elementId):

&lt;Table type={props.screenType === &quot;taskScreen&quot; ? &quot;tasks&quot; : &quot;calls&quot;} setElementId={setElementId /&gt;

In your child component, add a onSelectionChange and in the method, call props.SetElementId(data.id);:

const Table = (props) =&gt; {
const onSelectionChange = useCallback(({data}) =&gt; {
props.setElementId(data.id);
}, [])
return (
&lt;div&gt;
&lt;ReactDataGrid
idProperty=&quot;id&quot;
columns={headers}
dataSource={allTasks}
theme=&quot;default-light&quot;
rtl={true}
scrollThreshold={false}
enableColumnAutosize={true}
onRowChange={onSelectionChange} // don&#39;t forget to attach it
/&gt;
&lt;/div&gt;
);
};

huangapple
  • 本文由 发表于 2023年6月29日 18:15:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定