'App.js' 代码
还有其他几个不同的文件,所以如果有人需要查看文件结构,请告诉我。
import React from 'react';
import CoinList from './components/CoinList/CoinList';
import AccountBalance from './components/AccountBalance/AccountBalance';
import styled from 'styled-components';
import ExchangeHeader from './components/ExchangeHeader/ExchangeHeader';
const Div = styled.div`
text-align: center;
background-color: #373c46;
color: rgba(255, 255, 255, 0.842);
`;
class App extends React.Component {
constructor(props) {
super(props)
this.handleRefresh = this.handleRefresh.bind(this);
this.state = {
balance: 10000,
showBalance: true,
coinData: [
{
name: 'Bitcoin',
ticker: 'BTC',
balance: 1.5,
price: 9999.99
},
{
name: 'Ethereum',
ticker: 'ETH',
balance: 32.0,
price: 299.99
},
{
name: 'Tether',
ticker: 'USDT',
balance: 0,
price: 1.0
},
{
name: 'Ripple',
ticker: 'XRP',
balance: 1000,
price: 0.2
},
{
name: 'Bitcoin Cash',
ticker: 'BCH',
balance: 10,
price: 298.99
},
{
name: 'Cardano',
ticker: 'ADA',
balance: 20,
price: 1.56
},
]
}
this.handleRefresh = this.handleRefresh.bind(this);
this.handleBalanceVisibilityChange = this.handleBalanceVisibilityChange.bind(this);
};
handleBalanceVisibilityChange() {
this.setState(function (oldState) {
return {
...oldstate,
showBalance: !oldState.showBalance
}
});
}
handleRefresh(valueChangeTicker) {
const newCoinData = this.state.coinData.map(
function ({ticker, name, price}) {
let newPrice = price
if (valueChangeTicker === ticker) {
const randomPercentage = 0.995 + Math.random() * 0.01
newPrice = newPrice * randomPercentage
}
return {
name,
ticker,
price: newPrice
}
});
this.setState({coinData: newCoinData});
}
render() {
return (
<Div className="App">
<ExchangeHeader/>
<AccountBalance
amount={this.state.balance}
showBalance={this.state.showBalance}
handleBalanceVisibilityChange={this.handleBalanceVisibilityChange}/>
<CoinList
coinData={this.state.coinData}
showBalance={this.state.showBalance}
handleRefresh={this.handleRefresh}/>
</Div>
)
}
}
export default App;
我不确定如何解决此错误。我正在学习 React 的课程,它是几年前制作的,所以我认为可能与更新有关。
我也尝试了多种解决方案,但还是遇到了同样的问题。有人可以帮忙吗?