Ich bin neu in Redux und reagieren. Ich versuche, die Sortierung für eine reaktive Komponente zu implementieren, aber soweit ich weiß, dürfen Sie die props
in Redux nicht manipulieren. Was ist der richtige Weg, dass reagieren redux in tunWas ist die Redox-Reaktion Weg Requisiten in reagieren Kinder Komponente
Hier meine Komponente
import React, { Component, PropTypes } from 'react';
import _ from 'lodash';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import * as ArticlesActions from '../../actions/ArticlesActions';
import { ArticleList } from '../../components';
class ArticleListApp extends Component {
static propTypes = {
articles: PropTypes.array.isRequired,
next: PropTypes.string.isRequired,
actions: PropTypes.object.isRequired
};
componentWillMount() {
this.props.actions.fetchArticles('57bde6d6e4b0dc55a4f018e0');
}
renderLoadMoreButton() {
if (!_.isEmpty(this.props.next)) {
return (
<button type="button" className="btn btn-link center-block" onClick={this.props.actions.fetchMoreArticles.bind(this, this.props.next)}>Load More</button>
);
}
return '';
}
render() {
return (
<div>
<ArticleList articles={this.props.articles}/>
<div className="row">
<div className="col-md-12 center-block">
{this.renderLoadMoreButton()}
</div>
</div>
</div>
);
}
}
function mapStateToProps(state) {
return {
articles: state.articleList.articleList,
next: state.articleList.next
};
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(ArticlesActions, dispatch)
};
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(ArticleListApp);
einmal I articles
ich passieren, dass zu ArticleList
Komponente, die ich denke, es ist eine dump
Komponente in redux reagieren
import React, { Component, PropTypes } from 'react';
export default class ArticleList extends Component {
static propTypes = {
articles: PropTypes.array.isRequired
}
constructor(props, context) {
super(props, context);
}
renderList() {
return this.props.articles.map((article) =>
<tr>
<td>{article.title}</td>
<td>Author</td>
<td>{article.words}</td>
<td>ago</td>
</tr>
);
}
sortArticles() {
this.props.articles = this.props.articles.sort(); // I'm not allowed to do this.
}
render() {
return (
<table className="table table-hover">
<thead>
<tr>
<th>UNPUBLISHED ARTICLES ({this.props.articles.length})</th>
<th>AUTHOR</th>
<th>WORDS</th>
<th onClick={this.sortArticles.bind(this)}>SUBMITTED</th>
</tr>
</thead>
<tbody>
{this.renderList()}
</tbody>
</table>
);
}
}
In dieser Komponente möchte ich eine Sortierfunktion haben, wo ein Benutzer auf die Spalte klicken und die gesamte Tabelle nach dieser Spalte sortieren kann.
Das Beispiel articles
Objekt würde
[{title: 'title', word: 10}, {title: 'title', word: 5}]
Basierend auf welchem Attribut möchten Sie sie sortieren? 'this.props.articles.sort()' wird nicht funktionieren, weil ein Artikel ein Objekt ist. –
Sorry, ich stelle das nur als Beispiel dar, ich möchte nach einem Attribut sortieren, ich werde die Frage aktualisieren. – toy