forked from relyky/mvcReduxLab
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
70 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
mvcReduxLab/Scripts/React/ReduxLab/ReduxHello/InputMIrror.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, { Component } from 'react' | ||
import { connect } from 'react-redux' | ||
import actions, { Ks } from 'CommonFF/actions.js' | ||
|
||
class InputMirror extends Component { | ||
constructor(props) { | ||
super(props) | ||
|
||
this.handleInputChange = this.handleInputChange.bind(this) | ||
} | ||
|
||
render() { | ||
const { hello } = this.props | ||
return ( | ||
<div className="container"> | ||
<input name="hello" | ||
value={hello} | ||
onChange={this.handleInputChange} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
handleInputChange(e) { | ||
const target = e.target | ||
const value = target.type === 'checkbox' ? target.checked : target.value | ||
const name = target.name | ||
|
||
// 法1: | ||
const action = { type: Ks.ASSIGN_VALUE, name, value } | ||
this.props.dispatch(action) | ||
|
||
// 法2: | ||
this.props.dispatch(actions.assignValue(name, value)) | ||
|
||
// 法3: --- 過度包裝,除非有 高度共用性 | ||
this.props.handleValueChange(name, value) | ||
|
||
// 法4: *** 回歸最原生的方法,反而是綜合考量後的最佳解 | ||
this.props.dispatch({ type: Ks.ASSIGN_VALUE, name, value }) | ||
|
||
//this.setState({ | ||
// [name]: value | ||
//}) | ||
} | ||
|
||
} | ||
|
||
// connect to Store | ||
const mapStateToProps = (state, ownProps) => ({ | ||
hello: state.helloInfo.hello | ||
}) | ||
|
||
const targetReducer = 'helloReducer' | ||
const mapDispatchToProps = (dispatch, ownProps) => ({ | ||
dispatch, | ||
handleValueChange: (name, value) => dispatch({ type: Ks.ASSIGN_VALUE, name, value, targetReducer }) | ||
}) | ||
|
||
export default connect( | ||
mapStateToProps, | ||
mapDispatchToProps | ||
)(InputMirror); |