redux-react在react子组件中操作props的方法是什么?
What's the redux-react way to manipulate props in react children component
我是Redux和React的新手。我试图实现一个反应组件的排序,但据我所知,你不允许在Redux中操纵props
。在redux-react中,正确的方法是什么?
这是我的组件
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);
一旦我得到articles
,我将其传递给ArticleList
组分我认为它是还原反应中的dump
组分
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>
);
}
}
在这个组件中,我希望有一个排序功能,用户可以单击列并按该列对整个表进行排序。
articles
对象的例子是
[{title: 'title', word: 10}, {title: 'title', word: 5}]
如果我理解正确的话,那么有两种方法:
-
物品应保持在减速器中,排序或不排序,取决于用户交互。从技术上讲,这是通过有"排序"动作触发(由容器),然后减速器处理该事件通过改变其状态(返回一个新的状态,是以前的数组,但排序)。无论这是如何实现的,这都会引入一个问题:订阅了articles reducer的任何组件将始终获得一个排序列表。这是你想要的吗?也许"排序"显示是ui组件状态的一部分并且是特定于该组件的所以....
-
让减速器保持未排序的数组,并有两个不同的组件(或一个组件与一个额外的道具说明,如果它的"排序"或不),并在该组件的渲染功能,要么排序或不排序,根据组件容器给出的道具。从技术上讲,这是通过让容器组件有一个状态(已排序或未排序)来完成的,该状态作为prop传递给视图组件。
如果你是react/redux的新手,我说的对你来说没什么意义,我不介意添加代码-但我认为你的问题是关于原理的,而不是关于实际的代码。
所以Redux流组件将调用一个action, reducer将会监听这个action,从而更新状态。任何容器(封装在连接函数中的组件)都将使用新状态重新呈现自己。
所以你在这里要做的是有一个监听器来监听你的ArticleList组件(哑组件)中列的onClick事件,它向父组件ArticleListApp(容器,或智能组件)触发一个函数。这将导致容器触发一个动作,比如props。例如sortByColumn('AUTHOR', 'asc')。这个动作应该在你的actions文件中定义基本上是这样的
function sortByColumn(columnName, order) {
return {
type: 'SORT_BY_COLUMN',
columnName: columnName,
order: order
}
}
Reducer将监听该操作,并使用新订单中列出的商品更新您的商店。
当更新发生时,侦听该状态的容器将使用新状态重新呈现自身。你可以把点击处理程序放在每一列上,你想调用父列和它想排序的列。
你的组件看起来像这样:
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}
handleSortByCol={this.props.sortByColumn(columnName, sortOrder)} />
<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);
另一个哑组件是:
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.handleSortByCol('author', 'asc');
}
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>
);
}
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- 数组在递归方法中设置为null
- 打破承诺链的好方法是什么
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 使用“;这个“;JavaScript原型方法中的关键字
- 序列化数据属性中对象的最可靠方法
- 使用Objective-C的JavaScript注入方法
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 有没有一种方法可以防止img get请求使用css或js发生
- Javascript,有没有一种方法可以将数组写成没有逗号或空格的单个文本字符串
- 通过 props 与方法调用传递数据做出反应
- 错误:无法读取property 'props'在Child中调用父方法
- 在Vue.js 2中,将props作为初始数据传递的正确方法是什么?
- 在react.js中处理未定义的props的最好方法是什么?
- redux-react在react子组件中操作props的方法是什么?
- 是否有一个React生命周期方法,只在组件第一次接收到props时才做一些事情?
- React Native:将DataSource从_renderRow中的Props传递到方法中
- 反应:从渲染方法外部更新组件的 props,而不使用状态
- 在React中调用this.props.children上的setState或其他方法是一个好习惯吗?