Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0630 #5

Open
LeungKaMing opened this issue Jul 1, 2017 · 0 comments
Open

0630 #5

LeungKaMing opened this issue Jul 1, 2017 · 0 comments

Comments

@LeungKaMing
Copy link
Owner

LeungKaMing commented Jul 1, 2017

Action

// action通常是一个对象,在异步时会是一个函数
{
    type: 'userAction', // 肯定会有type属性
    ...state // 其他扩展属性;state是一个对象,这里用到es6解构语法,把key-value对拷贝到当前对象
}

在真实App,最聪明的做法就是给每个Action构建最好尽量带上index来标记。

{
  type: 'userAction',
  index: 5,
  ...state
}

Action Creator

传统Flux的Action Creator:

function actionCreater (text) {
  const temp = {
    type: 'USER_ACTION',
    ...text
  }
  dispatch(temp)
}

但是Redux的Action Creator是这样:

// action creator
function addTodo (text) {
  return {
    type: 'USER_ACTION',
    ...text
  }
}

// 虽然不像Flux,但是可以模拟Flux做自动dispatch,唯一只做的就是调用actionCreater
const actionCreater = text => { dispatch(addTodo(text)) }

// 调用
actionCreater({
  name: 'ljm'
})

其实到最后也是调用这个函数返回的对象,用于传去Reducer。

@LeungKaMing LeungKaMing mentioned this issue Jul 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant