[英]How to get text element of the given react component in React.cloneElement?
Developers give me the headings of the table:开发人员给我表的标题:
const CustomersTable = () => {
var headers=<>
<th>Name</th>
<th>Age</th>
<th>Another text</th>
</>
return <Table
headers={headers}
/>
}
And this is the code of the Table
component:这是
Table
组件的代码:
const Table = ({headers}) => {
var clonedHeaders = React.Children
.toArray(headers.props.children)
.map(header => React.cloneElement(header, {
className: "text-gray-900 py-3 font-light text-xs"
}));
return <table>
<thead>
<tr>
{clonedHeaders}
</tr>
</thead>
</table>
}
I can use React.cloneElement
to add attributes to the elements I receive as props of my component.我可以使用
React.cloneElement
向作为组件道具接收的元素添加属性。
However, I want to be able to change the text content of those received elements too.但是,我也希望能够更改那些接收到的元素的文本内容。
For example, I want to call my locale translation function on table header elements, automatically .例如,我想在表头元素上自动调用我的语言环境转换函数。 Right now, if developers want to make their tables multi-lingual, they should write this:
现在,如果开发人员想让他们的表格多语言,他们应该这样写:
var headers = <>
<th>{t('Name')}</th>
<th>{t('Age')}</th>
<th>{t('Other text')}</th>
</>
I want to centralize that t(text)
function for all headers
prop.我想集中所有
headers
道具的t(text)
函数。 Can I do that?我可以这样做吗?
You can use the same technique on the child elements of the headers as you do on the headers themselves:您可以在标题的子元素上使用与标题本身相同的技术:
const clonedHeaders = React.Children
.toArray(headers.props.children)
.map(header => React.cloneElement(header, {
className: "text-gray-900 py-3 font-light text-xs",
children: React.Children.toArray(header.props.children).map(child => {
return typeof child === "string" ? t(child) : child;
})
}));
Live Example:现场示例:
const {useState} = React; function t(english) { // Just so we can see that it happens return english.toLocaleUpperCase(); } const CustomersTable = () => { var headers=<React.Fragment> <th>Name</th> <th>Age</th> <th>Another text</th> </React.Fragment>; return <Table headers={headers} />; }; const Table = ({headers}) => { const clonedHeaders = React.Children .toArray(headers.props.children) .map(header => React.cloneElement(header, { className: "text-gray-900 py-3 font-light text-xs", children: React.Children.toArray(header.props.children).map(child => { return typeof child === "string" ? t(child) : child; }) })); return <table> <thead> <tr> {clonedHeaders} </tr> </thead> </table>; }; ReactDOM.render(<CustomersTable />, document.getElementById("root"));
<div id="root"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>
That example doesn't do any recursion, so it won't handle <th><span className="something">Name</span></th>
.该示例不进行任何递归,因此不会处理
<th><span className="something">Name</span></th>
。 If you want to handle that, you'll have to write a recursive function to handle it, but it'll be along the same lines.如果你想处理它,你必须编写一个递归函数来处理它,但它会沿着相同的路线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.