简体   繁体   English

在 ReactJS 中,如何在另一个组件中渲染一个组件?

[英]In ReactJS, how do I render one component within another?

I'm writing a ReactJS app, and want to show a landing page which displays either a Sign In form or a Register form, depending on which link was followed.我正在编写一个 ReactJS 应用程序,并希望显示一个登录页面,该页面显示登录表单或注册表单,具体取决于所访问的链接。

So far, my App component looks like this:到目前为止,我的 App 组件如下所示:

const App = () => (
  <Router>
    <div>
      <Route path={ROUTES.LANDING}  render={(props) => <LandingPage {...props} subPage={SignInPage} />}/>
      <Route path={ROUTES.SIGNIN}   render={(props) => <LandingPage {...props} subPage={SignInPage} />}/>
      <Route path={ROUTES.REGISTER} render={(props) => <LandingPage {...props} subPage={RegisterPage} />}/>
      <Route render={(props) => <LandingPage {...props} subPage={SignInPage} />}/>
    </div>
  </Router>
);

Now, I'm not sure how to get the actual sub-pages to show within the landing page.现在,我不确定如何让实际的子页面显示在登录页面中。

In my landingpage component, I have this:在我的登录页面组件中,我有这个:

class LandingPage extends Component {
  constructor(props) {
    super(props);

    this.state = {props}

  }

  render() {

    return (
      <div className="wrapper">
        <div>{this.state.subPage}</div>
      </div>
    );
  }
}

export default LandingPage;

but it doesn't show anything when I go to 'landingpage/signin'.但是当我将 go 设置为“登陆页面/登录”时,它没有显示任何内容。 I can see in the console that this.state.subPage contains the correct component, and when I change the Route to show the SignInPage directly, that works fine, so the SignInPage is definitely working ok on its own.我可以在控制台中看到 this.state.subPage 包含正确的组件,并且当我更改 Route 以直接显示 SignInPage 时,它工作正常,因此 SignInPage 本身肯定可以正常工作。

My SignInPage component contains:我的 SignInPage 组件包含:

const SignInPage = () => (
  <div>
    <p>Sign In</p>
    <SignInForm />
  </div>
);

class SignInFormBase extends Component {
  constructor(props) {
    super(props);

  }

  onSubmit = event => {
    // This will handle form submission
  };

  render() {
    return (
      <form onSubmit={this.onSubmit}>
        <input name="email" placeholder="Email Address" />
        <input name="password" type="password" placeholder="Password" />
        <button type="submit">Sign In</button>
      </form>
    );
  }
}
const SignInForm = compose(
  withRouter,
  withFirebase,
)(SignInFormBase);

export default SignInPage;

export { SignInForm };

I'm guessing that maybe the issue is that SignInForm is a component but SignInPage isn't?我猜可能问题是 SignInForm 是一个组件,但 SignInPage 不是? But I'm new to React, so I don't know how to reconfigure them!但是我是 React 的新手,所以我不知道如何重新配置它们!

What can I try next?接下来我可以尝试什么?

Since you are passing the subPage to the LandingPage as props, you can take that component from props and render it like below由于您将LandingPage作为道具传递给subPage ,因此您可以从道具中获取该组件并将其呈现如下

class LandingPage extends Component {
  constructor(props) {
    super(props);

  }

  render() {
    const SubPage = this.props.subPage;
    return (
      <div className="wrapper">
        <div><SubPage /></div>
      </div>
    );
  }
}

export default LandingPage;

NOTE: You don't need to copy props into state注意:您不需要将道具复制到 state

In App component:在 App 组件中:

<Route render={(props) => <LandingPage {...props}><SignInPage /></LandingPage>}/>

In landingpage component:在登录页面组件中:

<div className="wrapper">
 <div>{this.props.children}</div>
</div>

Instead of explicitly passing the components as props to LandingPage component, you can nest them inside the LandingPage component无需将组件作为props显式传递给LandingPage组件,您可以将它们嵌套在LandingPage组件中

<LandingPage {...props}>
   <SignInPage />
</LandingPage>

SignIn component will be passed to the LandingPage component as props and will be accessible using props.children SignIn组件将作为 props 传递给LandingPage组件,并且可以使用props.children访问

return (
  <div className="wrapper">
    {this.props.children}
  </div>
);

An advantage of this approach is that you can nest as many components inside LandingPage component and they all will be rendered inside LandingPage component using this.props.children这种方法的一个优点是您可以在LandingPage组件中嵌套尽可能多的组件,并且它们都将使用this.props.childrenLandingPage组件中呈现

Your routes will look like this after this change更改后您的路线将如下所示

<Route path={ROUTES.LANDING} render={(props) => <LandingPage {...props}> <SignInPage/> </LandingPage> }/>
<Route path={ROUTES.SIGNIN} render={(props) => <LandingPage {...props}> <SignInPage/> </LandingPage>}/>
<Route path={ROUTES.REGISTER} render={(props) => <LandingPage {...props}> <RegisterPage/> </LandingPage>}/>
<Route render={(props) => <LandingPage {...props}> <SignInPage/> </LandingPage>}/>

暂无
暂无

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

相关问题 ReactJS,如何在另一个组件的render方法中渲染一个组件? - ReactJS, how to render a component within another component's render method? 为什么ReactJS不将一个组件渲染到另一个组件中? - Why do not ReactJS render one component inside another? 使用 Reactjs 获取 GET 请求 - 如何在我的请求中实现可重用组件来呈现我的数据? - Fetch GET request with Reactjs - how do i implement a reusable component within my request to render my data? 如何使用过滤的道具渲染 ReactJS 组件? - How do I render ReactJS component with filtered props? 如何将道具从一个组件发送到渲染之外的另一个组件? - How do I send props from one component to another component, outside the render? Reactjs-如何将数据或变量从一个组件传递到另一个组件 - Reactjs- How do I pass data or a variable from one component to another 如何在 React(Next.js) 的一个组件中的一个文件夹中呈现文件夹? - How do I render folders within a folder in one component in React(Next.js)? 我如何通过比较 ReactJs 中的另一个数组来呈现数组值 - How do i render the Array value by comparing another array in ReactJs 我如何在功能组件 reactJS 中获得一个 for 循环 - How do I get a for loop working within a functional component reactJS ReactJs:如何简化这个组件,这样我就不必渲染我的 Modal 和 window 组件两次? - ReactJs: How can I simplify this component so that I do not have to render my Modal and window component twice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM