//HeaderBar.jsx 子组件
import React, { Component } from 'react';
class Header extends Component {
constructor() {
super();
this.handleClick = (e) => {
console.log(this)
}
}
renderLeftComponent() {
let leftDOM = {};
if (this.props.renderLeftComponent) {
return this.props.renderLeftComponent();
}
if (this.props.showBack) {
let backFunc = this.props.onBack || this.goBack;
leftDOM = (<a onClick={backFunc.bind(this)}><i className="icon left-icon icon-left-arrow"></i></a>);
}
return leftDOM;
}
renderRightComponent() {
if (this.props.renderRightComponent) {
return this.props.renderRightComponent();
}
}
goBack() {
alert("返回上一页")
}
render() {
return (
<header className="header-bar">
{this.renderLeftComponent()}
<span>{this.props.title || '滴滴'}</span>
{this.renderRightComponent()}
</header>
);
}
}
export default Header;
//父亲组件部分代码App.jsx
import HeaderBar from "./components/Header";
let leftIcon = function () {
return (
<a><i className="icon left-icon icon-left-haha"></i>左边按钮</a>
)
}
class App extends Component {
render() {
return (
<div className="App">
<HeaderBar title="滴滴打车" renderLeftComponent={leftIcon} />
</div>
);
}
}
// Component 父级
class parentComponent extends React.Component {
// add the following property
static childContextTypes = {
color: React.PropTypes.string
}
// 添加下面方法
getChildContext() {
return {
color: "#f00"
}
}
render() {
<div>
<Child1 />
</div>
}
}
// Component Child1
class Child1 extends React.Component {
// 添加下面属性
static contextTypes = {
color: React.PropTypes.string
}
render() {
<div>{this.context.color}</div>
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有