简体   繁体   English

React Native将一个组件传递给另一组件

[英]React Native pass one component to another component

Am new to the React world , so am facing issues with communications between the components. 这是React世界的新手,因此面临组件之间的通信问题。 We have a 3 step login screen , and each screen has a top banner image and main background image , also few links in the footer .and the only difference is that in each screen we will have different form fields. 我们有一个三步登录屏幕,每个屏幕都有一个顶部横幅图像和主要背景图像,页脚中也有很少的链接。唯一的区别是在每个屏幕中,我们将有不同的表单字段。 lets say in the first screen we will have user name and a button , on the second screen some label and a text fields , and in the last screen we will have password fields and a image . 可以说,在第一个屏幕中,我们将有用户名和一个按钮,在第二个屏幕中,我们将有一些标签和一个文本字段,在最后一个屏幕中,我们将有密码字段和一个图像。

So what am planning is that i want to create a Index component with the common elements like i said the header logo, and the main image , and the footer . 因此,我正在计划要创建一个Index组件,该组件具有一些通用元素,例如我所说的页眉徽标,主图像和页脚。 and i want to pass the components of the 3 screens into that so that Index will render the passed components , so if i pass the first component it should display the header logo , main image ,footer and the first screen component . 而且我想将3个屏幕的组件传递到其中,以便Index将呈现传递的组件,因此,如果我通过第一个组件,则应显示标题徽标,主图像,页脚和第一个屏幕组件。

This is the first component which is calling Index and passing the LoginForm ,but its not rendering the passed LoginForm component, it just display the images of the Index component only. 这是第一个调用Index并传递LoginForm的组件,但它不呈现传递的LoginForm组件,它仅显示Index组件的图像。 How can i display the passed LoginForm within the Index component ? 如何在Index组件中显示传递的LoginForm?

import { LoginForm } from "./shared-components/LoginForm";
import { Index } from "./shared-components/Index";

export class Login extends Component {
  constructor(prop) {
    super(prop);
    this.state = { username: "", password: "", result: [], isLoading: false };
  }

  render() {
    return <Index passedForm={LoginForm} />;
  }
}

This is the Index component 这是索引组件

import React from "react";
import { Image, ImageBackground } from "react-native";
import { Container, Content, View } from "native-base";
export class Index extends React.Component {
  constructor(prop) {
    super(prop);
    this.state = { username: "", password: "", result: [], isLoading: false };
  }


  render() {
    return (
      <Container>
        <Content>
          <Image
            source={require("../assets/finastra_logo.jpg")}
            style={{
              maxHeight: 150,
              alignSelf: "center",
              maxWidth: 215,
              flex: 1
            }}
          />
          <ImageBackground
            resizeMode="cover"
            blurRadius={3}
            source={require("../assets/frond.jpg")}
            style={{ alignSelf: "stretch", height: 500, marginBottom: 20 }}
          >
            {this.props.passedForm}
          </ImageBackground>
        </Content>
      </Container>
    );
  }
}

You could fix it by passing the <LoginForm /> component instead of LoginForm . 您可以通过传递<LoginForm />组件而不是LoginForm来修复它。

But this is a good use case for the children property: 但这对于children属性是一个很好的用例:

In Login.js, you can render: 在Login.js中,您可以呈现:

render() {
  return (
    <Index>
      <LoginForm />
    </Index>
  );
}

Then you can display the form in Index.js using: 然后,您可以使用以下命令在Index.js中显示表单:

{this.props.children}

You can pass it to variable and then use that variable to render 您可以将其传递给变量,然后使用该变量进行渲染

const PassedForm = this.props.passedForm;

And then render it like you would usually render components 然后像通常渲染组件一样渲染它

<PassedForm />

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

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