babel-plugin-jsx icon indicating copy to clipboard operation
babel-plugin-jsx copied to clipboard

Migrate jsx compoents

Open Edge00 opened this issue 4 years ago • 2 comments

Some of my components are written in jsx (vue@2) For example:

<script>
  import isArray from 'lodash/isArray'
  import isFunction from 'lodash/isFunction'
  import map from 'lodash/map'
  import { NewBsBox } from '@myComp'

  export default {
    name: 'Paragraph',
    functional: true,
    props: {
      content: {
        type: [String, Function, Array],
      },
      bs: {
        type: Object,
        default: () => ({}),
      },
    },
    render(h, { props }) {
      const { content, bs } = props
      let lst = content
      if (!isArray(lst)) {
        lst = [lst]
      }
      return (
        <NewBsBox bs={bs}>
          {
            map(lst, txt => {
              let text = txt
              if (isFunction(text)) {
                text = text(h)
              }
              return <NewBsBox>{text}</NewBsBox>
            })
          }
        </NewBsBox>
      )
    },
  }
</script>

If migrate to vue@3. Do i have to rewrite all my jsx components?

Edge00 avatar Sep 14 '20 10:09 Edge00

@Amour1688 PTAL Thanks !

Edge00 avatar Sep 15 '20 07:09 Edge00

You don't need to rewrite it at all. Here is the breaking change:

  1. Global render function
- render(h) {
+ render() {
  return (
    <AnchoredHeading level={1}>
      <span>Hello</span> world!
    </AnchoredHeading>
  )
}
  1. { on, props, attrs, ... } configuration is flattened
const vcUploadProps = {
-  props: {
-    ...this.$props,
-   prefixCls,
-    beforeUpload: this.reBeforeUpload,
-  },
-  on: {
-    start: this.onStart,
-    error: this.onError,
-    progress: this.onProgress,
-    success: this.onSuccess,
-    reject: this.onReject,
- },
+  ...this.$props,
+  prefixCls,
+  beforeUpload: this.reBeforeUpload,
+  onStart: this.onStart,
+  onError: this.onError,
+  onProgress: this.onProgress,
+  onSuccess: this.onSuccess,
+  onReject: this.onReject,
+  ref: 'uploadRef',
+  attrs: this.$attrs,
+  ...this.$attrs,
};

And you can refer to the documentation here

Amour1688 avatar Sep 15 '20 07:09 Amour1688