我正在創建一個reactJS應用程序,它將顯示手機列表。我目前正在調用API,將狀態設置爲響應,然後顯示內容。我現在正在尋找添加排序順序,但有困難。ReactJS - 組件來改變另一個呈現的內容
當用戶進入/手機/蘋果/ iPhone,路由將呈現以下組件調用API,設置狀態,將數據傳遞到結果組件...
export default class Phones extends Component {
constructor(props) {
super(props);
this.state = {
data: null,
make: this.props.params.make || null,
model: this.props.params.model || null
};
}
componentDidMount(){
const makeQuery = this.state.make ? this.state.make + '/' : '';
const modelQuery = this.state.model ? this.state.model : '';
const fetchUrl = '/api/phones/' + makeQuery + modelQuery;
fetch(fetchUrl, {
headers: {
Accept: 'application/json'
}
}).then(response => {
if (response.ok) {
response.json().then(json=> {
this.setState({data: json});
});
}
});
}
render() {
if (this.state.data) {
const currentUrl = this.props.location.pathname;
return (
<section className="phones">
<Filters
data={this.state.data}
currentUrl={currentUrl} />
<Results
data={this.state.data} />
</section>
)
}
}
}
的結果組件將映射數據並呈現電話列表。
在Filters組件中,我有一個下拉菜單,允許用戶按價格值對結果進行排序,這也設置了狀態。
export default class Filters extends Component {
constructor(props){
super(props);
this.state = {
value: 0
}
this.onChange = this.onChange.bind(this);
}
onChange(e){
this.setState({value: e})
}
render() {
return (
<div>
<p>Price</p>
<select onChange={this.onChange}>
<option value='asc'>low to high</option>
<option value='desc'>high to low</option>
</select>
</div>
);
}
}
我遇到的問題是,我不知道我怎麼能這個過濾器適用於這使得結果的分量,什麼是去了解這一點的最好方法是什麼?
我已經開始閱讀終極版,但也很困惑,如果手機的數據和過濾器應該是在商店,因爲它是暫時的,如果他們去到另一個頁面,即/手機將改變/三星/銀河/
任何幫助表示讚賞
那太好了,謝謝你一個偉大的例證。我已經閱讀了文檔並理解(我認爲),對於涉及API的示例尤其有幫助。 – Phil