Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // actions.js
  2.  
  3. function searchAction(search_string) {
  4.     return {
  5.         type: 'SEARCH_SMTH',
  6.         payload: search_string
  7.     }
  8. }
  9.  
  10. // {your component}.js
  11.  
  12.  
  13.  
  14. // ....
  15. import { bindActionCreators } from 'redux'
  16. // ....
  17. import * as MyActions from "../actions.js";
  18. // ....
  19.  
  20. class YourComponent extends Component {
  21.     constructor(props) {
  22.         super(props);
  23.         this.state = {
  24.             value: ""
  25.         }
  26.     }
  27.  
  28.     handleChange(event) {
  29.         this.setState({value: event.target.value});
  30.     }
  31.    
  32.     onSearch() {
  33.         this.props.myActions.searchAction(this.state.value)
  34.     }
  35.    
  36.  
  37.     render() {
  38.         return (
  39.             // тут форма
  40.             <input type="text" value={this.state.value} onChange={this.handleChange} />
  41.             <button onClick={this.onSearch}>Найти</button>
  42.         );
  43.     }
  44. }
  45.  
  46. function mapDispatchToProps(dispatch) {
  47.     return {
  48.         myActions: bindActionCreators(MyActions, dispatch),
  49.     }
  50. }
  51.  
  52. function mapStateToProps( state ) {
  53.     return {};
  54. }
  55.  
  56. export default connect(mapStateToProps, mapDispatchToProps)(YourComponent);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement