Reactjs 我可以在渲染后将两个proptypes添加到一起吗?如果是这样,我如何才能做到这一点?
()我有一个带有道具的div,我想根据一个道具的数量是否大于另一个道具来显示它。在这个特殊的组件中,我有很多事情要做,我担心下面我尝试做的所有事情都是不可能的Reactjs 我可以在渲染后将两个proptypes添加到一起吗?如果是这样,我如何才能做到这一点?,reactjs,if-statement,react-proptypes,Reactjs,If Statement,React Proptypes,()我有一个带有道具的div,我想根据一个道具的数量是否大于另一个道具来显示它。在这个特殊的组件中,我有很多事情要做,我担心下面我尝试做的所有事情都是不可能的 this.props.currentValue
this.props.currentValue
import React, {PropTypes, Component} from 'react';
import Header from '../compare-table-header/compare-table-header';
import './compare-table-row.css';
export class Rates extends Component {
constructor(props) {
super(props);
this.displayThing = this.displayThing.bind(this);
}
displayThing() {
const increase = <div>{this.props.details}</div>;
const thing = <div>hi</div>;
if (this.props.currentValue < this.props.newValue) {
return increase;
} else {
return thing;
}
}
render() {
const {currentValue, newValue} = this.props;
return (
<div>
<Header heading="Rates" />
<div className="value-heading">{currentValue}</div>
<div className="value-heading">{newValue}</div>
</div>
<div>{this.displayThing()}</div>
</div>
</div>
</div>
);
}
}
Rates.propTypes = {
currentValue: PropTypes.number,
newValue: PropTypes.number
};
import React,{PropTypes,Component}来自'React';
从“../compare table Header/compare table Header”导入标题;
导入“./compare table row.css”;
出口类费率扩展组件{
建造师(道具){
超级(道具);
this.displayThing=this.displayThing.bind(this);
}
displayThing(){
常量增加={this.props.details};
const thing=hi;
if(this.props.currentValue
带有this.displayThing的行没有呈现任何内容,因为您正在向函数本身传递引用,而不是调用函数并呈现它返回的值
如果您将this.displayThing
更改为this.displayThing()
,那么该行应该可以实现您所期望的功能
但是你也有一些不匹配的标签。标题组件在同一行上打开和关闭。从缩进中看,它看起来像是要将它下面的行呈现为Header组件的子级,但实际情况并非如此
你可以这样清理:
return (
<div>
<Header heading="Rates">
<div className="value-heading">{currentValue}</div>
<div className="value-heading">{newValue}</div>
</Header>
<div>{this.displayThing()}</div>
</div>
);
return (
<div>
<Header heading="Rates" />
<div className="value-heading">{currentValue}</div>
<div className="value-heading">{newValue}</div>
<div>{this.displayThing()}</div>
</div>
);
export class Rates extends Component {
displayThing = () => {
const increase = <div>{this.props.details}</div>;
const thing = <div>hi</div>;
if (this.props.currentValue < this.props.newValue) {
return increase;
} else {
return thing;
}
}
// ... rest of the class
}
您可以将displayThing
变成一个箭头函数,并去掉构造函数,如下所示:
return (
<div>
<Header heading="Rates">
<div className="value-heading">{currentValue}</div>
<div className="value-heading">{newValue}</div>
</Header>
<div>{this.displayThing()}</div>
</div>
);
return (
<div>
<Header heading="Rates" />
<div className="value-heading">{currentValue}</div>
<div className="value-heading">{newValue}</div>
<div>{this.displayThing()}</div>
</div>
);
export class Rates extends Component {
displayThing = () => {
const increase = <div>{this.props.details}</div>;
const thing = <div>hi</div>;
if (this.props.currentValue < this.props.newValue) {
return increase;
} else {
return thing;
}
}
// ... rest of the class
}
导出类速率扩展组件{
displayThing=()=>{
常量增加={this.props.details};
const thing=hi;
if(this.props.currentValue
无论哪种方式,该类的工作方式都是相同的,但它保存了几行代码。带有this.displayThing的行没有呈现任何内容,因为您正在传递对函数本身的引用,而不是调用函数并呈现它返回的值 如果您将
this.displayThing
更改为this.displayThing()
,那么该行应该可以实现您所期望的功能
但是你也有一些不匹配的标签。标题组件在同一行上打开和关闭。从缩进中看,它看起来像是要将它下面的行呈现为Header组件的子级,但实际情况并非如此
你可以这样清理:
return (
<div>
<Header heading="Rates">
<div className="value-heading">{currentValue}</div>
<div className="value-heading">{newValue}</div>
</Header>
<div>{this.displayThing()}</div>
</div>
);
return (
<div>
<Header heading="Rates" />
<div className="value-heading">{currentValue}</div>
<div className="value-heading">{newValue}</div>
<div>{this.displayThing()}</div>
</div>
);
export class Rates extends Component {
displayThing = () => {
const increase = <div>{this.props.details}</div>;
const thing = <div>hi</div>;
if (this.props.currentValue < this.props.newValue) {
return increase;
} else {
return thing;
}
}
// ... rest of the class
}
您可以将displayThing
变成一个箭头函数,并去掉构造函数,如下所示:
return (
<div>
<Header heading="Rates">
<div className="value-heading">{currentValue}</div>
<div className="value-heading">{newValue}</div>
</Header>
<div>{this.displayThing()}</div>
</div>
);
return (
<div>
<Header heading="Rates" />
<div className="value-heading">{currentValue}</div>
<div className="value-heading">{newValue}</div>
<div>{this.displayThing()}</div>
</div>
);
export class Rates extends Component {
displayThing = () => {
const increase = <div>{this.props.details}</div>;
const thing = <div>hi</div>;
if (this.props.currentValue < this.props.newValue) {
return increase;
} else {
return thing;
}
}
// ... rest of the class
}
导出类速率扩展组件{
displayThing=()=>{
常量增加={this.props.details};
const thing=hi;
if(this.props.currentValue
无论哪种方式,该类的工作原理都是一样的,但它保存了几行代码。“this.props.currentValue