我正在创建一个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