- 主要知识点:组件通信(父子传参,子父传参,非父子传参{兄弟传参})
- App.js
import React, {
Component } from 'react'
import ToDoContent from "./toDoContent"
import Add from "./add"
import "./style.css"
class App extends Component {
constructor(props) {
super(props);
this.state = {
};
}
render() {
return <div className="wrapper">
<h3>toDoList</h3>
<Add />
<ToDoContent />
</div>
}
}
export default App;
*{
margin:0;
padding:0;
list-style: none;
}
#root,html,body{
height:100%;
width:100%;
}
.wrapper{
height:100%;
width:100%;
display:flex;
flex-direction: column;
overflow: hidden;
}
.wrapper>h3{
height:40px;
width:100%;
line-height: 40px;
text-align: center;
background:salmon;
}
.add>input{
height:30px;
width:90%;
margin-left:5%;
border:1px solid #ccc;
margin-top:8px;
text-indent: 10px;
font-size:16px;
}
.list{
margin-left:15px;
margin-top:10px;
}
.inputs{
margin-top:5px;
}
.inputs>span{
font-size:18px;
margin-left:5px;
}
.inputs>button{
height:30px;
width:45px;
line-height:30px;
text-align: center;
font-size: 15px;
margin-left:6px;
}
import React, {
Component } from 'react'
import bus from "../utils/bus"
class Add extends Component {
constructor(props) {
super(props);
this.state = {
val: "",
};
}
render() {
let {
val } = this.state;
return <div className="add">
<input type="text" value={
val} onChange={
this.handChange.bind(this)}
onKeyDown={
this.handDown.bind(this)}
/>
</div>
}
handChange(e) {
this.setState({
val: e.target.value,
})
}
handDown(e) {
if (e.keyCode == 13) {
bus.emit("inputAdd", {
val: this.state.val, isChecked: false, status: "todo" })
this.setState({
val: ""
})
}
}
}
export default Add;
import React, {
Component } from 'react'
class List extends Component {
constructor(props) {
super(props);
this.state = {
};
}
render() {
let {
obj } = this.props;
return <div className="inputs"