react怎样断点调试

react进行断点调试的方法:

使用hooks进行简单断点调试

import React, { useState, useEffect, useReducer } from 'react';
import { useImmer } from "use-immer"
import './App.css';
 
 
function reducer(state: { count: number; }, action: { type: any; }) {
  switch (action.type) {
    case 'increment':
      return { count: state.count + 1 };
    case 'decrement':
      return { count: state.count - 1 };
    default:
      throw new Error();
  }
}
 
function App() {
  // 定义
  const [state, setState] = useImmer({
    people: [
      {
        name: '马云',
        englishName: 'Jack Ma'
      },
      {
        name: '马化腾',
        englishName: 'Pony Ma'
      },
      {
        name: '李彦宏',
        englishName: 'Robin Li'
      }
    ]
  })
 
  const [num, setNum] = useState(1);
 
  const [stateReducer, dispatchReducer] = useReducer(reducer, {count:999});
  useEffect(() => {
    console.log(num);
    //处理异步数据
  }, [num])
 
  useEffect(() => {
    console.log(state);
  }, [state.people[2]])
 
  const onClick = () => {
    setNum((num) => {
      console.log(num,"点击之前的变化");
      //处理异步数据
      return num = 2
    })
  }
  const onClick1 = () => {
    setState((state: { people: { name: string; }[]; }) => { state.people[2].name = '陈强' })
    console.log(state);//异步
  }
  const onClick2 = () => {
    dispatchReducer({ type:"increment"})
    console.log(stateReducer.count);//异步
  }
  return (
    <div className="App">
      <h1>我是useReducer----{stateReducer.count}</h1>
      <h1>{num}</h1>
      <ul>
        {state.people.map(item => <li key={item.englishName}>{item.name}</li>)}
      </ul>
      <button onClick={onClick}>点击</button>
      <button onClick={onClick1}>复杂数据处理</button>
 
      <button onClick={onClick2}>我要修改stateReducer</button>
    </div>
  );
}
 
export default App;

相关学习推荐:react视频教程

以上就是react怎样断点调试的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » React 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏