parent
9dad13636d
commit
92abaa5acc
4 changed files with 112 additions and 26 deletions
@ -0,0 +1,21 @@ |
||||
import React, { Component } from 'react'; |
||||
import Number from './number'; |
||||
import String from './string'; |
||||
import 'bootstrap/dist/css/bootstrap.css'; |
||||
|
||||
class App extends Component { |
||||
state = { } |
||||
render() { |
||||
return ( |
||||
<React.Fragment> |
||||
<div className="container"> |
||||
<Number></Number> |
||||
<hr /> |
||||
<String></String> |
||||
</div> |
||||
</React.Fragment> |
||||
); |
||||
} |
||||
} |
||||
|
||||
export default App; |
@ -0,0 +1,35 @@ |
||||
import React, { Component } from 'react'; |
||||
import {connect} from 'react-redux'; |
||||
class Number extends Component { |
||||
state = { } |
||||
handleClick = () => { |
||||
this.props.concat('h'); |
||||
} |
||||
render() { |
||||
console.log(this.props) |
||||
return ( |
||||
<React.Fragment> |
||||
<h3>Number组件: </h3> |
||||
<div>{this.props.number}</div> |
||||
<button className='btn btn-danger' onClick={this.handleClick}>添加字符</button> |
||||
</React.Fragment> |
||||
); |
||||
} |
||||
} |
||||
|
||||
const mapStateToProps = (state,props) => { |
||||
return { |
||||
number: state.number, |
||||
} |
||||
} |
||||
|
||||
const mapDispatchToProps = { |
||||
concat: (c) => { |
||||
return { |
||||
type: 'concat', |
||||
character: c, |
||||
} |
||||
} |
||||
} |
||||
|
||||
export default connect(mapStateToProps,mapDispatchToProps)(Number); |
@ -0,0 +1,45 @@ |
||||
import React, { Component } from 'react'; |
||||
import { connect } from 'react-redux'; |
||||
|
||||
class String extends Component { |
||||
state = { } |
||||
handleAdd = () => { |
||||
this.props.add(10); |
||||
} |
||||
|
||||
handleSub = () => { |
||||
this.props.sub(20); |
||||
} |
||||
render() { |
||||
return ( |
||||
<React.Fragment> |
||||
<h3>String组件: </h3> |
||||
<div>{this.props.string}</div> |
||||
<button className='btn btn-success' style={{marginRight: "10px"}} onClick={this.handleAdd}>add</button> |
||||
<button className='btn btn-info' onClick={this.handleSub}>sub</button> |
||||
</React.Fragment> |
||||
); |
||||
} |
||||
} |
||||
const mapStateToProps = (state,props) => { |
||||
return { |
||||
string: state.string, |
||||
} |
||||
} |
||||
|
||||
const mapDispatchToProps = { |
||||
add: (n) => { |
||||
return { |
||||
type: 'add', |
||||
value: n, |
||||
} |
||||
}, |
||||
sub: (n) => { |
||||
return { |
||||
type: 'sub', |
||||
value: n, |
||||
} |
||||
} |
||||
} |
||||
|
||||
export default connect(mapStateToProps,mapDispatchToProps)(String); |
Loading…
Reference in new issue