-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropsState.html
82 lines (76 loc) · 1.82 KB
/
PropsState.html
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>State</title>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
class NewsNo extends React.Component {
constructor (props){
super(props);
this.state={
isEditing:false,
description:props.children
};
this.inputText=React.createRef();
}
edit(){
this.setState({
isEditing:true
});
}
delete(){
alert("delete button clicked");
}
save(){
const inputValue=this.inputText.current.value;
this.setState({
isEditing:false,
description:inputValue
});
}
renderEdit(){
const isEditing = this.state.isEditing;
const props = this.props;
if (isEditing ===true){
return(
<React.Fragment>
<input defaultValue={props.children}
ref={this.inputText}
/>
<button onClick={()=>this.save()}>Save</button>
</React.Fragment>
)
}else{
return(<p>{this.state.description}</p>)
}
}
render() {
const props = this.props;
return(
<div>
<h1>{props.title}</h1>
{this.renderEdit()}
<div calssName="action">
<button className="edit" onClick={()=>this.edit()}>Edit</button>
<button className="delete" onClick={this.delete}>Delete</button>
</div>
</div>
)
}
}
ReactDOM.render(
<div>
<NewsNo title="News1">I play football.</NewsNo>
<NewsNo title="News2">I play Cricket.</NewsNo>
</div>,
document.getElementById('root')
);
</script>
</body>
</html>