/
10-Sending-values-between-components.js
59 lines (51 loc) · 1.44 KB
/
10-Sending-values-between-components.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import React from 'react';
import ReactDOM from 'react-dom';
let container = document.getElementById('app');
class Box extends React.Component {
constructor(){
super()
this.state = {
imageSize: 'small' // initial state
}
this.update = this.update.bind(this)
}
update(value){
this.setState({
imageSize: value
})
}
render(){
return(
<div style={this.props.style}>
<Avatar url={this.props.imageUrl} className={this.state.imageSize}/>
<div>
<p>{this.props.username}</p>
<Sizer value={this.state.imageSize} onValueChange={this.update}/>
</div>
</div>
);
}
}
class Avatar extends React.Component {
render(){
return(
<img src={this.props.url} ref="avatarSize" className={this.props.className} style={{marginRight: '10px'}}/>
);
}
}
class Sizer extends React.Component {
handleChange(e) {
let value = e.target.value // get event value
this.props.onValueChange(value) // send value to the component
}
render(){
return(
<select ref="sel" value={this.props.value} onChange={this.handleChange.bind(this)}>
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
</select>
);
}
}
ReactDOM.render(<Box username="Kitten 1" imageUrl="https://placekitten.com/80/80" style={{display:'flex', backgroundColor: '#f1f1f1'}}/>, container);