简体   繁体   English

在React.js的同一页面中调用多个组件

[英]Calling Multiple component in same page in React.js

I am creating a react main page which is rendering two react component as 我正在创建一个反应主页,将两个反应组件呈现为

render() {
    return (
        <Header />             
        <Test />
    );
}

Header is having simple static content.In Test I am calling external api using redux on page page load as 标头具有简单的静态内容。在测试中,我使用页面页面加载时的redux调用外部api,

componentWillMount() {
    if (this.props.onPageLoadTest) {
        this.props.onPageLoadTest();
    }
}
render() {
    const { data } = this.props;
    return (
        <div>
            {
                data.map((a) => (

                    <div key={a.id}>{a.id}

                    </div>

                ))
            }
        </div>
    );
}

Using props I am showing content in Test component.Header and Test are working fine when I am rendering them separately. 使用道具时,我在Test组件中显示内容。当我分别渲染它们时,Header和Test可以正常工作。
When I am trying to combine then only Header is showing but Test is not able to fetch data from API. 当我尝试合并时,仅显示Header,但Test无法从API提取数据。

You can't do things like this: 您不能做这样的事情:

render() {
    return (
        <Header />             
        <Test />
    );
}

there can be only one markup in the return of render() return render()只能有一个标记

if you want to render the Header and Test together here, you have to wrap them with one markup,like this: 如果要在此处将Header和Test一起渲染,则必须用一个标记将它们包装起来,如下所示:

render() {
    return (
        <div>
            <Header />             
            <Test />
        </div>
    );
}

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

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