markdown-to-jsx icon indicating copy to clipboard operation
markdown-to-jsx copied to clipboard

Why is props.children kept always as an array when using overrides?

Open desmap opened this issue 5 years ago • 2 comments

React usually keeps props.cihldren as a single string and not as an array if it contains just one element. Is there a reason? Problem is code consistency: You need to have a condition for components to render within and outside of <Markdown>'s overrides if you do sophisticated stuff with props.children.

Check the sandbox' console: https://codesandbox.io/s/reverent-bas-fp4m6

desmap avatar Mar 02 '20 05:03 desmap

I solved it with the option below. Now, also props that previously came as arrays are also fixed to strings. Also rendering of MD within JSX keeps working.

Still wondering why an array is returned with the default implementation or is it a bug?

      ...
      option={{
        createElement: (type, props, children) =>
          React.createElement(
            type,
            props,
            children && children.length === 1 ? children[0] : children
          ),
      ...

desmap avatar Mar 02 '20 08:03 desmap

I have the same issue. Trying to override native <a> link with next/link component but got the following error:

Warning: Failed prop type: Invalid prop children of type array supplied to NextLink, expected object.

BorysShulyak avatar Sep 23 '21 07:09 BorysShulyak