当前位置:   article > 正文

react---todoList案例

react---todoList案例

todoList案例效果图

 1.组件拆分

2.操作state数据

state 放在哪个组件:

  • 如果某个组件组件使用: 放在其自身的state中。
  • 如果某些组件使用:放在他们共同的父组件state中(官方称此操作为状态提升)

状态(state)在哪,操作状态的方法就在哪里。

App.jsx维护状态(数据),因此操作状态的新增,删除,选择等方法 要放在App.jsx中

  1. addTodo = (todoObj) => {
  2. console.log("App接收的子组件Header传来的参数", todoObj);
  3. const { todoList } = this.state;
  4. const newList = [todoObj, ...todoList];
  5. this.setState({ todoList: newList });
  6. };
  7. <Header addTodo={this.addTodo}></Header>

3.父子通信

父子通信:父->子通过props传递;子->父通过props传递,要求父提前给子传递一个函数

  1. //App.jsx
  2. allCheck = (done) => {
  3. const { todoList } = this.state;
  4. console.log("接受子组件footer的值", done);
  5. const newList = todoList.map((item) => {
  6. return { ...item, done: done };
  7. });
  8. this.setState({ todoList: newList });
  9. };
  10. deleteCheck = () => {
  11. const { todoList } = this.state;
  12. const newList = todoList.filter((item) => !item.done);
  13. this.setState({ todoList: newList });
  14. };
  15. render() {
  16. const { todoList } = this.state;
  17. return (
  18. <div className="container">
  19. <Footer
  20. todoList={todoList}
  21. allCheck={this.allCheck}
  22. deleteCheck={this.deleteCheck}
  23. ></Footer>
  24. </div>
  25. );
  26. }

父到子传递数据:App向Footer传递列表数据通过props传递,在Footer标签中通过【todoList={todoList}】传递todoList的数据,在Footer中通过【 const {todoList}=this.props】接收数据。

子到父传递数据:以全选为例,Footer要向App传递全选按钮的checked状态值为false还是true,需要事先在App定义好一个方法,并传递给Footer【allCheck={this.allCheck}】,在Footer中通过【 this.props.allCheck(event.target.checked)】传递数据

  1. export default class Footer extends Component {
  2. handleAllCheck=(event)=>{
  3. this.props.allCheck(event.target.checked)
  4. }
  5. handleDeleteCheck=()=>{
  6. this.props.deleteCheck()
  7. }
  8. render() {
  9. const {todoList}=this.props
  10. const count=todoList.length;
  11. // reduce用来累加
  12. // const doneCount=todoList.reduce((prev,current)=>{return prev+(current.done?1:0)},0)
  13. // 精简以上写法
  14. const doneCount=todoList.reduce((prev,current)=>prev+(current.done?1:0),0)
  15. return (
  16. <div className="footer-wrapper">
  17. <input type="checkbox" checked={count==doneCount&&count!=0?true:false} onChange={this.handleAllCheck} />
  18. <span>已完成{doneCount}/全部{count}</span>
  19. <button className="btn btn-danger" onClick={this.handleDeleteCheck}>清除已完成任务</button>
  20. </div>
  21. )
  22. }
  23. }

4.defaultChecked和checked区别

defaultChecked:仅在第一次初次渲染时生效,更新时不受控制;

checked:始终受到控制,必须通过绑定 onChange 事件来控制选中情况。

类似的还有:defaultValue和value

5.关于案例中一些js用法

reduce---用来实现计算已完成的个数

  1. // reduce用来累加
  2. // const doneCount=todoList.reduce((prev,current)=>{return prev+(current.done?1:0)},0)
  3. // 精简以上写法
  4. const doneCount=todoList.reduce((prev,current)=>prev+(current.done?1:0),0)
JavaScript中Reduce() 的6个用例

nanoid---生成唯一的id

  1. import {nanoid} from "nanoid"
  2. const obj={id:nanoid(),name:target.value,done:false}

通过{...item}批量传值

  1. <ul>
  2. {
  3. todoList.map(item=>{
  4. return <Item key={item.id} {...item} updateTodo={updateTodo} deleteTodo= {deleteTodo}/>
  5. })
  6. }
  7. </ul>

传统的 HTML通过【οnclick="demo()"】绑定事件,

在react中【onClick={this.demo}】

或者【onClick={this.demo()}】,加了()必须在demo还有一个回调函数

  1. handleMouse = (flag) => {
  2. // 注意这里要return一个回调,因为标签中调用时加了()
  3. return () => {
  4. this.setState({ mouse: flag });
  5. };
  6. };
  7. <button
  8. onClick={this.handleMouse(flag)}
  9. className="btn btn-danger"
  10. style={{ display: mouse ? "block" : "none" }}
  11. >
  12. 删除
  13. </button>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/223829
推荐阅读
相关标签
  

闽ICP备14008679号