简体   繁体   English

Object.keys()。map()的呈现方法未呈现

[英]Render method with Object.keys().map() not rendering

I've got the following code to render an accordion. 我有以下代码来呈现手风琴。 There are no syntax errors but it doesn't render any of the data. 没有语法错误,但不会呈现任何数据。 The json is correct and the data iteration part is happening correct. json是正确的,并且数据迭代部分正在正确进行。 If I add a return statement before the jsx elements it renders as perfect. 如果我在jsx元素之前添加return语句,那么它将呈现为完美。 What am I doing wrong? 我究竟做错了什么?

render(){
        const { activeIndex } = this.state
        const swaggerJson = this.props.swaggerJson;
        return (
            <Accordion>
                {Object.keys(swaggerJson.paths).map((pathName, pathIndex) => {
                    const pathObj = swaggerJson.paths[pathName];
                    return Object.keys(pathObj).map((key) => {
                        <Accordion.Title index={pathIndex} onClick={this.handleClick}>
                            <Icon name='dropdown' />
                            <span>{key}</span>
                            <span>{pathName}</span>
                            <span>{pathObj[key].summary}</span>
                        </Accordion.Title>,
                        <Accordion.Content active={activeIndex === pathIndex}>
                            <p>
                                A dog is a type of domesticated animal. {pathIndex} Known for its loyalty and faithfulness, it can
                                be found as a welcome guest in many households across the world.
                            </p>
                        </Accordion.Content>
                    });
                })}
            </Accordion>
        )
    }

I'm using babel and my .babelrc file. 我正在使用babel和我的.babelrc文件。

{
    "presets": ["es2015", "react"],
    "plugins": ["transform-object-rest-spread"]
}

You are not returning from the inner map method as well the mapped result 您不从内部map方法返回,也不从映射结果返回

render(){
        const { activeIndex } = this.state
        const swaggerJson = this.props.swaggerJson;
        return (
            <Accordion>
                {Object.keys(swaggerJson.paths).map((pathName, pathIndex) => {
                    const pathObj = swaggerJson.paths[pathName];
                    return Object.keys(pathObj).map((key) => {
                        return ([
                         <Accordion.Title index={pathIndex} onClick={this.handleClick}>
                            <Icon name='dropdown' />
                            <span>{key}</span>
                            <span>{pathName}</span>
                            <span>{pathObj[key].summary}</span>
                        </Accordion.Title>,
                        <Accordion.Content active={activeIndex === pathIndex}>
                            <p>
                                A dog is a type of domesticated animal. {pathIndex} Known for its loyalty and faithfulness, it can
                                be found as a welcome guest in many households across the world.
                            </p>
                        </Accordion.Content>
                       ])
                    });
                })}
            </Accordion>
        )
    }

You need to return the array of <Accordion.Title> component. 您需要返回<Accordion.Title>组件的数组。 Add return statement before Object.keys(pathObj).map((key) => { and also wrap the accordion title and content with a wrapper component. So the code will be like this Object.keys(pathObj).map((key) => {之前添加return语句,并使用包装器组件包装手风琴标题和内容。因此代码将像这样

 render(){
            const { activeIndex } = this.state
            const swaggerJson = this.props.swaggerJson;
            return (
                <Accordion>
                    {Object.keys(swaggerJson.paths).map((pathName, pathIndex) => {
                        const pathObj = swaggerJson.paths[pathName];
    //add return statement here
                        return Object.keys(pathObj).map((key) => 
// you need to wrap <Accordion.Title> and <Accordion.Content> using `div` or other component.
                          (<div> 
                            <Accordion.Title index={pathIndex} onClick={this.handleClick}>
                                <Icon name='dropdown' />
                                <span>{key}</span>
                                <span>{pathName}</span>
                                <span>{pathObj[key].summary}</span>
                            </Accordion.Title>,
                            <Accordion.Content active={activeIndex === pathIndex}>
                                <p>
                                    A dog is a type of domesticated animal. {pathIndex} Known for its loyalty and faithfulness, it can
                                    be found as a welcome guest in many households across the world.
                                </p>
                            </Accordion.Content>
                        <div>));
                    })}
                </Accordion>
            )
        }

Note: If you want to return something in the curly bracket({}), then there should be a return statement. 注意:如果要返回大括号({})中的内容,则应该有一个return语句。 In your current code, there is no return statement in the second map function 在您当前的代码中,第二个map函数中没有return语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM