gpt4 book ai didi

javascript - react : convert createClass to ES6 class

转载 作者:行者123 更新时间:2023-12-03 00:30:57 24 4
gpt4 key购买 nike

我想将带有 createClass 的代码转换为 ES6 类。当涉及到带有 getInitialState 函数的类时,我遇到了一些麻烦。

下面是我尝试转换的代码:

var StockRow = React.createClass({
render: function () {
var lastClass = '',
changeClass = 'change-positive',
iconClass = 'glyphicon glyphicon-triangle-top';
if (this.props.stock === this.props.last) {
lastClass = this.props.stock.change < 0 ? 'last-negative' : 'last-positive';
}
if (this.props.stock.change < 0) {
changeClass = 'change-negative';
iconClass = 'glyphicon glyphicon-triangle-bottom';
}
return (
<tr>
<td>{this.props.stock.symbol}</td>
<td>{this.props.stock.open}</td>
<td className={lastClass}>{this.props.stock.last}</td>
<td className={changeClass}>{this.props.stock.change} <span className={iconClass} aria-hidden="true"></span></td>
<td>{this.props.stock.high}</td>
<td>{this.props.stock.low}</td>
</tr>
);
}
});

var StockTable = React.createClass({
render: function () {
var items = [];
for (var symbol in this.props.stocks) {
var stock = this.props.stocks[symbol];
items.push(<StockRow key={stock.symbol} stock={stock} last={this.props.last} />);
}
return (
<div className="row">
<table className="table-hover">
<thead>
<tr>
<th>Symbol</th>
<th>Open</th>
<th>Last</th>
<th>Change</th>
<th>High</th>
<th>Low</th>
</tr>
</thead>
<tbody>
{items}
</tbody>
</table>
</div>
);
}
});

var HomePage = React.createClass({
getInitialState: function() {
var stocks = {};
feed.watch(['MCD', 'BA', 'BAC', 'LLY', 'GM', 'GE', 'UAL', 'WMT', 'AAL', 'JPM']);
feed.onChange(function(stock) {
stocks[stock.symbol] = stock;
this.setState({stocks: stocks, last: stock});
}.bind(this));
return {stocks: stocks};
},
render: function () {
return (
<div>
<StockTable stocks={this.state.stocks} last={this.state.last}/>
</div>
);
}
});

这是我前两个的内容:

class StockRow extends React.Component{
constructor(props, context) {
super(props, context);
}
render: function () {
var lastClass = '',
changeClass = 'change-positive',
iconClass = 'glyphicon glyphicon-triangle-top';
if (this.props.stock === this.props.last) {
lastClass = this.props.stock.change < 0 ? 'last-negative' : 'last-positive';
}
if (this.props.stock.change < 0) {
changeClass = 'change-negative';
iconClass = 'glyphicon glyphicon-triangle-bottom';
}
return (
<tr>
<td>{this.props.stock.symbol}</td>
<td>{this.props.stock.open}</td>
<td className={lastClass}>{this.props.stock.last}</td>
<td className={changeClass}>{this.props.stock.change} <span className={iconClass} aria-hidden="true"></span></td>
<td>{this.props.stock.high}</td>
<td>{this.props.stock.low}</td>
</tr>
);
}
}

class StockTable extends React.Component{
constructor(props, context) {
super(props, context);
}
render: function () {
var items = [];
for (var symbol in this.props.stocks) {
var stock = this.props.stocks[symbol];
items.push(<StockRow key={stock.symbol} stock={stock} last={this.props.last} />);
}
return (
<div className="row">
<table className="table-hover">
<thead>
<tr>
<th>Symbol</th>
<th>Open</th>
<th>Last</th>
<th>Change</th>
<th>High</th>
<th>Low</th>
</tr>
</thead>
<tbody>
{items}
</tbody>
</table>
</div>
);
}
}

但是,我不确定如何处理最终类,尤其是 getInitialState 函数。

如有任何帮助,我们将不胜感激!

编辑:

这是我第三堂课的内容:

class HomePage extends React.Component{
constructor(props, context) {
super(props, context);
this.state = {
stocks = {}
}

}
getData() {
var stocks = {};
feed.watch(['MCD', 'BA', 'BAC', 'LLY', 'GM', 'GE', 'UAL', 'WMT', 'AAL', 'JPM']);
feed.onChange(function(stock) {
stocks[stock.symbol] = stock;
this.setState({stocks: stocks, last: stock});
}.bind(this));
return {stocks: stocks};
}
componentDidMount() {
this.getData();
}
render: function () {
return (
<div>
<StockTable stocks={this.state.stocks} last={this.state.last}/>
</div>
);
}
}

最佳答案

您必须通过在构造函数中初始化状态变量来替换 getInitialState。

constructor(props, context) {
super(props, context);
state = {stocks}
}

如果你必须获取一些值来正确初始化状态,你可以在 componentDidMount 方法中完成

componentDidMount() {
fetchInfos().then( infos => this.setState({...infos}) );
}

关于javascript - react : convert createClass to ES6 class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53873309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com