简体   繁体   English

如何从另一个组件调用React的render方法()?

[英]How to call React's render method() from another component?

A client request a feature to implement dashboard switching. 客户端请求一项功能以实现仪表板切换。 I'm working on it: 我在做这个工作:

Dashboard.js Dashboard.js

import React, { Component } from 'react';
import { connect } from 'react-redux';

// components
import UserDashboard from '../components/dashboard/user-dashboard/UserDashboard.js';
import NewUserDashboard from '../components/new-dashboard/user-dashboard/NewUserDashboard.js';

@connect((state) => {
  return {
    identity: state.identity.toJS().profile
  };
})

export default class Dashboard extends Component {

  render() {
    const msisdn = this.props.location.state ? this.props.location.state.msisdn : null;
    return (
      <UserDashboard msisdn={ msisdn }/>
    );
  }
}

Dashboard.js is the dashboard controller. Dashboard.js是仪表板控制器。 I have 2 dashboards: UserDashboard, and NewDashboard. 我有2个仪表板:UserDashboard和NewDashboard。

Let's say an user is viewing another screen, and in that screen there's a button. 假设用户正在查看另一个屏幕,并且在该屏幕中有一个按钮。 If that button is clicked, the Dashboard will call it's render method, returning NewDashboard instead. 如果单击该按钮,则仪表板将调用它的render方法,而是返回NewDashboard。 And NewDashboard will be automatically displayed. 并且NewDashboard将自动显示。 Is this possible? 这可能吗?

Calling render method programmatically not possible. 编程方式无法调用render方法。

You have to do state update of that particular component if you want to call render method of that component. 如果要调用该组件的render方法,则必须对该组件进行state update

Say,if you want to call render method of Dashboard Component,you must call setState on this component. 例如,如果要调用Dashboard组件的渲染方法,则必须在此组件上调用setState You can do some dummy state lifting for that. 您可以为此进行一些虚拟状态提升。

Imagine you have this dashboard: 想象一下,您拥有以下信息中心:

function DashBoard({index}) {
  return index == 0 ? <UserDashBoard /> : <SecondDashBoard />;
}

Without a router: 没有路由器:

class ParentComponent extends ReactComponent {

  state = {
    dashboardIndex: 0
  }

  changeDashboard() {
    this.setState({
      dashBoardIndex: (state.dashboardIndex + 1) % 2
    })
  }

  render() {
    return (
        <div>
          <button onclick={() => this.changeDashboard()}>Change dashboard</button>
          <Dashboard index={this.state.dashboardIndex} />
        </div>
    )
  }  
}

With a router: 使用路由器:

<Switch>
  <Route match="/component1" component={UserDashboard} />
  <Route match="/component2" component={SecondDashboard} />
</Switch>

Also you can use redux. 您也可以使用redux。

Probably something like: 大概是这样的:

class NewDashboard extends React.Component {

  static triggerRender() {
    this.forceUpdate();
  }

  // or
  static altTriggerRender() {
    this.setState({ state: this.state });
  }

  render() {...}
}

Force React Component Render 强制反应组件渲染

Though, it's better to show/hide other components by conditional rendering. 不过,最好通过条件渲染来显示/隐藏其他组件。

Update: "This" is not accessible inside a static method. 更新:在静态方法内部无法访问“ This”。 Ignore the code. 忽略代码。

You can use conditional rendering using state. 您可以使用状态使用条件渲染。 You can keep track of currently active tab and use that state to render the desired component. 您可以跟踪当前活动的选项卡,并使用该状态来呈现所需的组件。

More often than not, in order to change page views, you would make use of Router. 通常,为了更改页面视图,您可以使用路由器。 You can configure Routes corresponding to Dashboard 您可以配置与仪表板相对应的路由

import UserDashboard from '../components/dashboard/user-dashboard/UserDashboard.js';
import NewUserDashboard from '../components/new-dashboard/user-dashboard/NewUserDashboard.js';

@connect((state) => {
  return {
    identity: state.identity.toJS().profile
  };
})

export default class Dashboard extends Component {
  render() {
    const msisdn = this.props.location.state ? this.props.location.state.msisdn : null;
    return (
       <BrowserRouter>
           <Route path="/dashboard/user" render={(props) => <UserDashboard msisdn={ msisdn } {...props}/>} />
           <Route path="/dashboard/new" render={(props) => <NewUserDashboard msisdn={ msisdn } {...props}/>} />
       </BrowserRouter>
    );
  }
}

and on button click you can use a link. 然后单击按钮就可以使用链接。

Or else you can conditionally render component based on state change 否则您可以根据状态变化有条件地渲染组件

// components
import UserDashboard from '../components/dashboard/user-dashboard/UserDashboard.js';
import NewUserDashboard from '../components/new-dashboard/user-dashboard/NewUserDashboard.js';

@connect((state) => {
  return {
    identity: state.identity.toJS().profile
  };
})

export default class Dashboard extends Component {
  state = {
      userDashboard: true
  }
  onToggle=(state)=> {
      this.setState(prevState => ({
          userDashboard: !prevState.userDashboard
      }))
  }
  render() {
    const msisdn = this.props.location.state ? this.props.location.state.msisdn : null;
    return <div>{userDashboard? <UserDashboard msisdn={ msisdn }/>
: <NewUserDashboard msisdn={ msisdn }/>}
         <button onClick={this.onToggle}>Toggle</button>
     </div>
    );
  }
}

暂无
暂无

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

相关问题 从render方法中的嵌套函数中反映调用组件的方法 - React call component's method from nested function in render method React:如何从另一个组件的自身渲染调用一个组件函数? - React: How to call a component function on it's own render from another component? 如何从React中的另一个组件调用组件的方法? - How can I call a component's method from another component in React? React如何从父组件调用子组件的方法 - How to call child component's method from a parent component in React 如果我从某个方法返回组件的 jsx 并在另一个组件的渲染中调用该方法,那么每次渲染时都会创建一个新对象吗? - If I return jsx of a Component from some method and call that method in another component's render, then is a new object created on every render? ReactJS,如何在另一个组件的render方法中渲染一个组件? - ReactJS, how to render a component within another component's render method? 如何从其他组件调用/执行方法/函数 - How to call/execute method/function from another component react 如何在React Native的初始渲染中多次调用组件的prop方法? - How to call a component's prop method several time on intial render in React Native? 如何从另一个组件调用一个组件方法? - How to call a component method from another component? 如何在React中的render方法之外的函数中渲染组件? - How to render component from a function outside the render method in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM