[英]How to replace jsx substring of a props - react
I am trying to create a shared table component.我正在尝试创建一个共享表组件。 And, on this table I am trying to make those edit url dynamic.
而且,在这张桌子上,我试图让那些编辑 url 动态。 Code is straight forward and loos like:
代码很简单,看起来像:
const actionColumn = (
<Link
to={`/suppliers/ROW_ID/edit`}
className="btn btn-info btn-xs"
title="Edit"
>
<i className="fas fa-pencil-alt"></i>
</Link>
);
<Table
actionColumn={actionColumn}
/>
Now on the table, I want to replace ROW_ID
with the row.id
现在在桌子上,我想用
ROW_ID
替换row.id
{list &&
list.map((row, i) => (
<tr>
<td>
{actionColumn.replace(
"ROW_ID",
row.id
)}
</td>
</tr>
))}
I tried to use .replace
but received an error: actionColumn.replace is not a function
我尝试使用
.replace
但收到错误: actionColumn.replace is not a function
First you need to make ActionColumn a component that accepts the id as props... Then export the component首先你需要让 ActionColumn 成为一个接受 id 作为 props 的组件...然后导出组件
const ActionColumn = ({ id }) => (
<Link
to={`/suppliers/${id}/edit`}
className="btn btn-info btn-xs"
title="Edit"
>
<i className="fas fa-pencil-alt"></i>
</Link>
);
export default ActionColumn;
In this component, you import the ActionColumn component and pass the row.id to it在此组件中,您导入 ActionColumn 组件并将 row.id 传递给它
{list &&
list.map((row, i) => (
<td>
<ActionColumn id={row.id} />
</td>
))}
Make a function instead, one that takes the to
prop to pass to the Link:制作一个 function 代替,将
to
道具传递给链接:
const ActionColumn = ({ to }) => (
<Link
to={to}
className="btn btn-info btn-xs"
title="Edit"
>
<i className="fas fa-pencil-alt"></i>
</Link>
);
For your original layout, you can use对于您的原始布局,您可以使用
<ActionColumn to={`/suppliers/ROW_ID/edit`} />
To render differently, pass a different prop:要进行不同的渲染,请传递不同的道具:
list.map((row, i) => (
<td>
<ActionColumn to={`/suppliers/${row.id}/edit`} />
</td>
))
Also note that you need to balance your <tr>
tags;另请注意,您需要平衡
<tr>
标签; you currently have a </tr>
but no matching <tr>
.您目前有一个
</tr>
但没有匹配的<tr>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.