gpt4 book ai didi

javascript - 将 react-markdown 与 Material-UI 表一起使用

转载 作者:行者123 更新时间:2023-12-03 23:05:36 25 4
gpt4 key购买 nike

我正在尝试使用 react-markdown 解析 Markdown 表并使用 Material-UI table component 渲染生成的标签.这是我的渲染器:

import withStyles from '@material-ui/core/styles/withStyles';
import PropTypes from 'prop-types';
import TableCell from '@material-ui/core/TableCell';

const styles = (theme) => ({
root: {
marginBottom: theme.spacing(2),
},
});

const RenderedTCell = ({rendererProps, classes}) => {
if(rendererProps.children.length !== 0) {
console.log('rendererProps.children', rendererProps.children);
}
return (
<TableCell className={classes.td} {...rendererProps} />
);
};

RenderedTCell.propTypes = {
classes: PropTypes.shape({
root: PropTypes.string,
}).isRequired,
};

export default withStyles(styles)(RenderedTCell);
所有表格标签的 Markdown 渲染器定义如下:
const renderers = {
tableCell: (props) => (
<RenderedTCell
rendererProps={props}
/>
),
tableRow: (props) => (
<RenderedTRow
rendererProps={props}
/>
),
tableBody: (props) => (
<RenderedTBody
rendererProps={props}
/>
),
tableHead: (props) => (
<RenderedTHead
rendererProps={props}
/>
),
table: (props) => (
<RenderedTable
rendererProps={props}
/>
),
};
在这里,所有其他元素( tabletableHeadtableBodytableRow )呈现正常,但 tableCell元素不断抛出错误:
Error while running `getDataFromTree` TypeError: Cannot read property 'charAt' of null
at capitalize (/home/ubuntu/proost/web/node_modules/@material-ui/core/utils/capitalize.js:19:17)
at Object.TableCell [as render] (/home/ubuntu/proost/web/node_modules/@material-ui/core/TableCell/TableCell.js:183:148)
at a.b.render (/home/ubuntu/proost/web/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:46:105)
at a.b.read (/home/ubuntu/proost/web/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:44:18)
at renderToStaticMarkup (/home/ubuntu/proost/web/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:54:462)
at process (/home/ubuntu/proost/web/node_modules/@apollo/react-ssr/lib/react-ssr.cjs.js:38:16)
at process._tickCallback (internal/process/next_tick.js:68:7)
知道什么可能会破坏代码吗?

最佳答案

我遇到过同样的问题。我通过不将所有 Prop 进一步传递给 Material 组件来解决它。
您可以在此 gist 的组件中看到我使用 Material ui 来主题化 Markdown 的内容:
https://gist.github.com/boganegru/a4da0b0da0b1233d30b10063b10efa8a

关于javascript - 将 react-markdown 与 Material-UI 表一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62923448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com