You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在React+TypeScript项目中如何为const函数添加console.log并实现count动态化

具体操作方法

添加console.log调试语句

你当前使用的是箭头函数隐式返回对象的简写语法,无法直接在返回逻辑中插入调试语句,需要先改成显式函数体+return的写法,再插入console.log:

export const customRow = (
  row: Partial<CustomComp>
): Partial<CustomComp> => {
  // 新增调试语句,可打印入参、执行状态等你需要的信息
  console.log('customRow接收到的入参row:', row);
  return {
    ...row,
    count: 1,
  };
};

将count调整为动态取值

根据你的业务场景可以选两种常用方案:

  • 方案1:优先从入参row中取count值,不存在时走默认值
export const customRow = (
  row: Partial<CustomComp>
): Partial<CustomComp> => {
  console.log('customRow接收到的入参row:', row);
  return {
    ...row,
    // 空值合并运算符,row.count为undefined/null时自动 fallback 到1
    count: row.count ?? 1,
  };
};
  • 方案2:新增独立的count入参,适合count值和row无关的场景
// 新增第二个可选参数,默认值设为1
export const customRow = (
  row: Partial<CustomComp>,
  count: number = 1
): Partial<CustomComp> => {
  console.log('入参row:', row, '指定count:', count);
  return {
    ...row,
    count,
  };
};

// 调用示例
// 用默认count:customRow(你的row对象)
// 传自定义count:customRow(你的row对象, 20)

内容的提问来源于stack exchange,提问作者hustle100

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 01:18:02