[英]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.