【问题标题】:React: Use component inside another componentReact:在另一个组件中使用组件
【发布时间】:2018-10-01 20:13:33
【问题描述】:

我是新手,所以我希望这些问题不会太愚蠢。

简而言之:我想在另一个组件道具中使用某个组件。 但它并没有像预期的那样工作。

我想在以下组件中使用Material UI CardsLink to repo of "Material-UI Tree View"

为了测试我写了以下代码:

import React,{Component} from 'react';
import SplitPane from "react-split-pane";
import MuiTreeView from 'material-ui-treeview';
import Demo from './demo';


export class App extends Component {
    render() {

        const tree = [
            {
                value: 'Parent A',
                nodes: [{ value: Demo}]
            }

        ];
        return (
            <SplitPane split="vertical"  defaultSize={500} allowResize={false}>

                <div><MuiTreeView tree={tree} /></div>
                <div id="mynetwork"></div>
            </SplitPane>
        );
    }
}

导入

import Demo from './demo';

是简单卡片的例子:https://codesandbox.io/s/3vl744v6z5

在我的 index.js 中,我像这样渲染 App 组件:

ReactDOM.render(
    <App />,
    document.getElementById('root')
);

我希望你能给我一个提示在哪里看看。

提前致谢,

结肠

【问题讨论】:

    标签: javascript reactjs react-component


    【解决方案1】:

    MuiTreeView 期望树的每个叶节点上都有一个 .value 成员,这是要呈现的 字符串。它根本不支持以这种方式注入自定义组件。

    你可以通过查看它的prop-types来判断

    【讨论】:

    • 感谢您的提示。以后我会记得查看 prop-types 的。
    【解决方案2】:

    不能,就像在MuiTreeView的源文件中检查一样,tree prop只能是一个字符串,或者递归一个对象,其中props值是字符串......

    由于您使用的是 MaterialUI 组件,请尝试检查 MUI 提供的expansion panels 并尝试获得所需的结果。在这种情况下,无需将组件作为道具传递。

    【讨论】:

    猜你喜欢
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 2019-10-02
    • 2017-07-05
    相关资源
    最近更新 更多