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

Update TodoHeader.js #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion Ch07/react-redux-example/src/components/TodoHeader/TodoHeader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { connect } from 'react-redux';

// 將欲使用的 actions 引入
import {
changeText,
createTodo,
} from '../../actions';

const mapStateToProps = (state) => ({
// 從 store 取得 todo state
todo: state.getIn(['todo', 'todo'])
});

const mapDispatchToProps = (dispatch) => ({
// 當使用者在 input 輸入資料值即會觸發這個函數,發出 changeText action 並附上使用者輸入內容 event.target.value
onChangeText: (event) => (
dispatch(changeText({ text: event.target.value }))
),
// 當使用者按下送出時,發出 createTodo action 並清空 input
onCreateTodo: () => {
dispatch(createTodo());
dispatch(changeText({ text: '' }));
}
});

export default connect(
mapStateToProps,
mapDispatchToProps,
)(TodoHeader);

// 開始建設 Component 並使用 connect 進來的 props 並綁定事件(onChange、onClick)。注意我們的 state 因為是使用 `ImmutableJS` 所以要用 `get()` 取值
const TodoHeader = ({
onChangeText,
onCreateTodo,
Expand All @@ -13,4 +43,4 @@ const TodoHeader = ({
</div>
);

export default TodoHeader;
export default TodoHeader;