import { connect } from 'react-redux';import MyField from '../presentation/MyField';import ActionCreator from '../actions/action-creators';
function mapStateToProps(state) {return {resetFocus: state.form.resetFocus}}
function mapDispatchToProps(dispatch) {return {clearResetFocus() {dispatch(ActionCreator.clearResetFocus());}}}
export default connect(mapStateToProps, mapDispatchToProps)(MyField);
演示组件
import React, { PropTypes } form 'react';
export default class MyField extends React.Component {// don't forget to .bind(this)constructor(props) {super(props);this._handleRef = this._handleRef.bind(this);}
// This is not called on the initial render so// this._input will be set before this get calledcomponentDidUpdate() {if(!this.props.resetFocus) {return false;}
if(this.shouldfocus()) {this._input.focus();this.props.clearResetFocus();}}
// When the component mounts, it will save a// reference to itself as _input, which we'll// be able to call in subsequent componentDidUpdate()// calls if we need to set focus._handleRef(c) {this._input = c;}
// Whatever logic you need to determine if this// component should get focusshouldFocus() {// ...}
// pass the _handleRef callback so we can access// a reference of this element in other component methodsrender() {return (<input ref={this._handleRef} type="text" />);}}
Myfield.propTypes = {clearResetFocus: PropTypes.func,resetFocus: PropTypes.bool}