Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从其他组件中的映射传递div数据_Javascript_Html_Reactjs - Fatal编程技术网

Javascript 从其他组件中的映射传递div数据

Javascript 从其他组件中的映射传递div数据,javascript,html,reactjs,Javascript,Html,Reactjs,我正在使用旋转木马组件创建测试组件。 在Carousel组件中,我从props传递带有数据的div export default class Test extends Component { render() { const { testData } = this.props; return ( <div> <Carousel>

我正在使用旋转木马组件创建测试组件。
在Carousel组件中,我从props传递带有数据的div

export default class Test extends Component {
    render() {

        const { testData } = this.props;
        return (
                <div>
                        <Carousel>
                        {
                            testData.length && testData.map((a) => (
                                    <div><img src=
                                        {a.link} />
                                    </div>

                                )
                            )
                        }



                        </Carousel>

            </div>

        );
    }
}

testData = [{"link":"/test1.jpg"},
{"link":"/test2.jpg"},
{"link":"/test3.jpg"}
]
导出默认类测试扩展组件{
render(){
const{testData}=this.props;
返回(
{
testData.length&&testData.map((a)=>(
)
)
}
);
}
}
testData=[{“link”:“/test1.jpg”},
{“link”:“/test2.jpg”},
{“link”:“/test3.jpg”}
]
当我这样做的时候,它工作的很好

 <div> <img src="/test1.jpg" /></div>
 <div> <img src="/test2.jpg" /></div>
 <div> <img src="/test3.jpg" /></div>


我使用testData的错误之处。

JSX中不允许使用常规JavaScript注释:

//<div> <img src="/test1.jpg" /></div>
//<div> <img src="/test2.jpg" /></div>
//<div> <img src="/test3.jpg" /></div>
import React,{Component}来自“React”
从“react bootstrap”导入{Carousel}
导出默认类测试扩展组件{
render(){
const testData=[{“link”:“/test1.jpg”},
{“link”:“/test2.jpg”},
{“link”:“/test3.jpg”}]
返回(
{testData.length&&testData.map((a)=>
(
))}
);
}
}

这段代码运行良好,因此我认为问题在于如何通过道具传递测试数据

如果您可以提供将测试数据作为道具传递的代码,则可以找到解决方案。

我终于解决了这个问题。 在第一种情况下,当我使用静态数据作为

testData = [{"link":"/test1.jpg"},
{"link":"/test2.jpg"},
{"link":"/test3.jpg"}
]
每次呈现组件时,它都会显示到页面上。 但在第二种情况下

const { testData } = this.props;
testData是通过API调用设置的。所以当组件第一次呈现时,它不会被提取。为了解决这个问题,我做了如下操作

if (!caurosalData.length) {
            return null;
        }

现在它工作正常

我正在尝试显示“testData”的数据。我正在通过删除此注释进行测试。我不理解您遇到的问题。:)如何将测试数据传递给组件?与David Tryon提出的问题相同。在那里,它工作得很好。
const { testData } = this.props;
if (!caurosalData.length) {
            return null;
        }