美文网首页
判断内容为空值的React节点

判断内容为空值的React节点

作者: 景阳冈大虫在此 | 来源:发表于2023-02-15 11:03 被阅读0次

目标

希望判断一个React节点的value是否是null这种空值,以便替换为符号-

原理

React Element 支持使用 node.props.value的方式获得节点的内容,但是React node有很多类型

React node
要先判断一下再取值,使用React.isValidElement(children)判断是否为React Element。

代码

import * as React from 'react';

export const EMPTY_SYMBOL = '-';

const checkEmpty = (val: any) => (val === undefined || val === null || val === '');

export const noBlankElement = (children: React.ReactNode, blankSymbol: React.ReactNode = EMPTY_SYMBOL): '-' | React.ReactNode => {
  try {
    if (checkEmpty(children)) return blankSymbol;
    if (React.isValidElement(children)) {
      if (checkEmpty(children.props?.value)) {
        const renderType = (children as React.ReactElement)?.type;
        if (typeof renderType === 'function') {
          const element = (renderType as Function)(children);
          if (checkEmpty(element)) return blankSymbol;
          if (element.type === React.Fragment && checkEmpty(children.props.children)) {
            return blankSymbol;
          }
        }
      }
    }
  } catch (e) {
    return children;
  }
  return children;
};

单测:

import * as React from 'react';
import { renderTest, screen } from 'test/lib/helpers/intlTestHelper';
import { noBlankElement, EMPTY_SYMBOL } from 'components/qualityFlow/components/Table/utils/handler';

const testWrapper = (children: React.ReactNode) => renderTest(<>{noBlankElement(children)}</>);

// eslint-disable-next-line react/jsx-no-useless-fragment
const FragmentComponent = () => <>{null}</>;

describe('handler noBlankElement', () => {
  describe('primitives', () => {
    it('check empty string', () => {
      testWrapper('');
      expect(screen.queryByText(EMPTY_SYMBOL)).toBeInTheDocument();
    });

    it('check null', () => {
      testWrapper(null);
      expect(screen.queryByText(EMPTY_SYMBOL)).toBeInTheDocument();
    });

    it('check undefined', () => {
      testWrapper(undefined);
      expect(screen.queryByText(EMPTY_SYMBOL)).toBeInTheDocument();
    });

    it('check number 0', () => {
      testWrapper(0);
      expect(screen.queryByText(EMPTY_SYMBOL)).not.toBeInTheDocument();
    });

    it('check false', () => {
      testWrapper(false);
      expect(screen.queryByText(EMPTY_SYMBOL)).not.toBeInTheDocument();
    });
  });

  describe('ReactElement', () => {
    it('render empty React.Fragment', () => {
      testWrapper(<FragmentComponent />);
      expect(screen.queryByText(EMPTY_SYMBOL)).toBeInTheDocument();
    });

    it('render element directly', () => {
      testWrapper(<div />);
      expect(screen.queryByText(EMPTY_SYMBOL)).not.toBeInTheDocument();
    });
  });
});

Reference:
https://beta.reactjs.org/reference/react/isValidElement
https://stackoverflow.com/questions/29568721/getting-dom-node-from-react-child-element

相关文章

  • 随笔记录的小技巧

    freemarker 判断空值 ${student.age!'ppp'} 如果student.age为空值...

  • empty 和 isset

    empty 判断变量的值或者数组是不是空 为空返回true,不为空返回false $str的值为 0 '0' ''...

  • 判断值是否为空

  • 基础

    判断是否为空 方法设置默认值

  • BST判断

    判断BST:1.假如二叉树的左子树不为空,则左子树上所有节点的值均小于它的根节点的值; 假如右子树不空,则右子树上...

  • mysql取得空值null置为零

    背景: 开发过程中遇到取数据为空值,而后需要对该空字段进行算数运算,于是在后台进行了判断空值,并为空值赋值为0。最...

  • 1-5空类型和智能类型转换

    空类型安全问题判断值是否为空,如果为空,则直接返回空,否则,返回相应值 null类型 声明可null类型但本身不为...

  • Python列表语法技巧

    1,列表判空一般的判断方法是: 简洁的判断方法是: 列表为空相当于布尔值False,非空相当于布尔值True 2,...

  • 判断对象属性值为空

    今天遇到的问题似曾相识,却费劲脑子怎么也想不起来了。 var a = {}; 如何判断这个对象的属性值为空,刚开始...

  • 大顶堆:堆中每个节点的值都大于等于其子节点中每个节点的值。本文内容以大顶堆为前提。 小顶堆:堆中每个节点的值都小于...

网友评论

      本文标题:判断内容为空值的React节点

      本文链接:https://www.haomeiwen.com/subject/vbgykdtx.html