什么是Redux及其工作原理
Redux 是 React的一个状态管理库,它基于flux。 Redux简化了React中的单向数据流。 Redux将状态管理完全从React中抽象出来。
它是如何工作的
在React中,组件连接到 redux ,如果要访问 redux,需要派出一个包含 id
和负载(payload) 的 action
。action 中的 payload
是可选的,action 将其转发给 Reducer。
当reducer
收到action
时,通过 swithc...case
语法比较 action
中type
。 匹配时,更新对应的内容返回新的 state
。
当Redux
状态更改时,连接到Redux
的组件将接收新的状态作为props
。当组件接收到这些props
时,它将进入更新阶段并重新渲染 UI。
Redux 循环细节
让我们详细看看整个redux 循环细节。
Action: Action 只是一个简单的json对象,type 和有payload作为键。type 是必须要有的,payload是可选的。下面是一个 action 的例子。
// action
{
type:"SEND_EMAIL",
payload: data
};
Action Creators:这些是创建Actions
的函数,因此我们在派发action
时不必在组件中手动编写每个 action
。 以下是 action creator 的示例。
// action creator
export function sendEamil(data) {
return { type:"SEND_EMAIL", payload: data};
}
Reducers:Reducers 是纯函数,它将 action
和当前 state
作为参数,计算必要的逻辑并返回一个新r的state
。 这些 Reducers 没有任何副作用。 它不会改变 state
而是总是返回 state
。
export default function emailReducer(state = [], action){
switch(action.type) {
case "SEND_EMAIL": return Object.assign({}, state, {
email: action.payload
});
default: return state;
}
}
组件如何与 redux
进行连接
mapStateToProps:此函数将state
映射到 props
上,因此只要state
发生变化,新 state 会重新映射到 props
。 这是订阅store
的方式。
mapDispatchToProps:此函数用于将 action creators
绑定到你的props
。以便我们可以在第12
行中使用This . props.actions.sendemail()
来派发一个动作。
connect
和bindActionCreators
来自 redux。 前者用于连接 store ,如第22行,后者用于将 action creators 绑定到你的 props
,如第20行。
// import connect
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
// import action creators
import * as userActions from '../../../actions/userActions';
export class User extends React.Component {
handleSubmit() {
// dispatch an action
this.props.actions.sendEmail(this.state.email);
}
}
// you are mapping you state props
const mapStateToProps = (state, ownProps) => ({user: state.user})
// you are binding your action creators to your props
const mapDispatchToProps = (dispatch) => ({actions: bindActionCreators(userActions, dispatch)})
export default connect(mapStateToProps, mapDispatchToProps)(User);
什么是 React Router Dom 及其工作原理
react-router-dom
是应用程序中路由的库。 React库中没有路由功能,需要单独安装react-router-dom
。
react-router-dom 提供两个路由器BrowserRouter
和HashRoauter
。前者基于rul的pathname段,后者基于hash段。
前者:http://127.0.0.1:3000/article/num1
后者:http://127.0.0.1:3000/#/article/num1(不一定是这样,但#是少不了的)
react-router-dom 组件
BrowserRouter
和 HashRouter
是路由器。
Route
用于路由匹配。
Link
组件用于在应用程序中创建链接。 它将在HTML中渲染为锚标记。
NavLink
是突出显示当前活动链接的特殊链接。
Switch
不是必需的,但在组合路由时很有用。
Redirect
用于强制路由重定向
下面是组件中的Link
、NavLink
和Redirect
的例子
// normal link
<Link to="/gotoA">Home</Link>
// link which highlights currentlu active route with the given class name
<NavLink to="/gotoB" activeClassName="active">
React
</NavLink>
// you can redirect to this url
<Redirect to="/gotoC" />
以下是 react router 组件的示例。 如果你查看下面的示例,我们将匹配路径并使用Switch
和Route
呈现相应的组件。
import React from 'react'
// import react router DOM elements
import { Switch, Route, Redirect } from 'react-router-dom'
import ComponentA from '../common/compa'
import ComponentB from '../common/compb'
import ComponentC from '../common/compc'
import ComponentD from '../common/compd'
import ComponentE from '../common/compe'
const Layout = ({ match }) => {
retu