我基本上符合FCC的工程之一完成(https://www.freecodecamp.com/challenges/build-a-camper-leaderboard)簡單的背景顏色的變化作出反應,終極版
唯一的定製是,我想爲用戶,看看錶,從有序(或最近或全部最佳成績)。
目前,我有它使用jQuery告訴當用戶單擊近期或alltime柱直接更改標題表CSS屬性,但我不覺得這是正確的方式。
這裏基本上保持表本身
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux'
import { recentData } from '../actions/index'
import { allTimeData } from '../actions/index'
export default class TableBoard extends Component {
constructor(props){
super(props);
}
componentDidMount() {
//Is there better way to initialize the background-color???
$("#col-recent").css("background-color", "lightblue");
this.props.recentData() //fetch data most recent top
}
renderData(userData,index){
const FCC_URL = 'https://www.freecodecamp.com/'
const img = userData.img
const name = userData.username;
const recent = userData.recent;
const allTime = userData.alltime;
return(
<tr key={name}>
<td className="rank">{index + 1}</td>
<td>
<img className="img-responsive"
src={img}
alt='FCC profile'
/> <a href={FCC_URL + name}>{name}</a>
</td>
<td className="recent">{recent}</td>
<td className="all-time">{allTime}</td>
</tr>
)
}
getRecentData(){
$("#col-recent").css("background-color", "lightblue");
$("#col-alltime").css("background-color", "#e2e2e2");
this.props.recentData()
}
getAllTimeData(){
$("#col-alltime").css("background-color", "lightblue");
$("#col-recent").css("background-color", "#e2e2e2");
this.props.allTimeData();
}
render(){
return(
<table className="table table-hover table-striped table-bordered">
<thead>
<tr>
<th className="rank">#</th>
<th className="username">Camper Name</th>
<th id='col-recent'className="recent clickable"
onClick={this.getRecentData.bind(this)}
>Points in 30 days
</th>
<th id='col-alltime' className="all-time clickable"
onClick={this.getAllTimeData.bind(this)}
>All-time Posts
</th>
</tr>
</thead>
<tbody>
{this.props.FCCData.map(this.renderData)}
</tbody>
</table>
)
}
}
function mapStateToProps(state){
return {
FCCData: state.collectData
}
}
function mapDispatchToProps(dispatch){
return bindActionCreators({ recentData, allTimeData }, dispatch)
}
export default connect(mapStateToProps, mapDispatchToProps)(TableBoard);
我還是新來我的容器代碼陣營,終極版這樣的狀態的模式仍然是新的我。
有沒有問題....或什麼?你到底在問什麼? – Quill
嗯,我猜我的問題是有沒有使用jQuery設置背景顏色的另一種方法? 截至目前,該頁面從API服務器收到請求之前,列「近期」已經突出顯示(下componentDidMount) – Alejandro