使用redux制作单页应用程序并做出反应,首先要做什么? 使用redux首先创建我的状态的逻辑,或者让所有组件首先作出反应?Building React/Redux应用程序
回答
可能首先要做的是建立你的减速器功能。 这里是一个例子。我使用ES6的例子。
const INCREMENT = 'redux-example/counter/INCREMENT';
const initialState = {
count: 0
};
export default function reducer(state = initialState, action = {}) {
switch (action.type) {
case INCREMENT:
const {count} = state;
return {
count: count + 1
};
default:
return state;
}
}
export function increment() {
return {
type: INCREMENT
};
}
然后,你必须创建一个组件:
import React, {Component, PropTypes} from 'react';
import {connectMultireducer} from 'multireducer';
import {increment} from 'redux/modules/counter';
@connectMultireducer(
(key, state) => ({count: state.multireducer[key].count}),
{increment}
)
export default class CounterButton extends Component {
static propTypes = {
count: PropTypes.number,
increment: PropTypes.func.isRequired,
className: PropTypes.string
}
props = {
className: ''
}
render() {
const {count, increment} = this.props; // eslint-disable-line no-shadow
let {className} = this.props;
className += ' btn btn-default';
return (
<button className={className} onClick={increment}>
You have clicked me {count} time{count === 1 ? '' : 's'}.
</button>
);
}
}
在你的组件,你会减速状态和动作连接到您的组件,然后将它们包装成一个容器,并将其链接到HTML。
希望这会有所帮助。
我认为这在很大程度上取决于您是否更好地理解数据或更好地了解您的UI。我会写出一个容器组件,并将所有数据作为状态传递给它,然后将其映射到道具。从那里你可以决定哪些组件需要状态树的哪一部分。然后,您可以通过跟踪数据来了解推理组件设计。
这里有一个简单的例子,我勾勒出:https://github.com/matthewkturner/redux-simple-boilerplate
嗯,我认为理解用户界面更好。然后,您可以在之后理解数据。虽然这只是我的看法,因为我只是一个纯粹的前端开发人员,对后端知之甚少(php) – MeetMahPuppy
对于一个职业的立场(没有人会认为不赞成),他们通常做什么?我无法找到一个很好的教程/例子来说明两者。 – MeetMahPuppy
你会犯错误先做所有组件或所有州第一。
只需从1个组件开始并将其挂接到redux。不要编写不需要的状态或组件。
更好地从必要的代码开始,如果需要更改,修改/重构该代码。
当你有一个假设是不正确的,并且事实证明你必须重写你预先写好的组件时,预优化是痛苦的。在项目过程中,您总是会找到更好的方法来处理事情,因此当时您拥有的组件和/或减少器的数量就会越多。
- 1. Building C Runtime随程序ANTLR
- 2. Xcode 4 building但未运行OpenFrameworks示例应用程序
- 3. 在.net桌面应用程序中集成Building Wayfinder
- 4. Interface Building中的更改未显示在应用程序
- 5. 为什么不定义ReactRedux?
- 6. 使用Windows SDK 10.0.10586.0 Building Building Boost 1.60
- 7. Building Zimbra
- 8. WebDriver on building-machine
- 9. Building ACE_SSL
- 10. Building Windows Clusters
- 11. building tshark
- 12. Building TestLink
- 13. Python scons building
- 14. Heroku - Git push without building
- 15. 应用程序错误heroku节点应用程序
- 16. Building Select Statement
- 17. building solr查询
- 18. Building gcc-4.8
- 19. Scala List building
- 20. Symofony building subform
- 21. TinyMCE Plugin building
- 22. Sql query building
- 23. Building Custom Extentable Models
- 24. extractin/building boost program_options
- 25. Jenkins on GCE not building
- 26. MSBuild not building csproj
- 27. javascript jQuery object building
- 28. KeyError:0L building boxplot
- 29. Gradle Building Non-Stop
- 30. Linq XML Dynamic building
嗨,我一直在学习与es2015,和@(运营商?)有点新的面貌给我,请解释一下? – MeetMahPuppy
@是装饰器,它可以让你编写更少的代码来完成redux连接。这里有一个链接来更详细地解释它。 http://stackoverflow.com/questions/32646920/whats-in-the-redux-connect-decorator –