react-jsx-parser icon indicating copy to clipboard operation
react-jsx-parser copied to clipboard

Extend limited support for object spreading

Open andrewkshim opened this issue 3 years ago • 0 comments

Hi @TroyAlford, thanks for this library. I have a use case where I'd like to spread data objects in the jsx, e.g.:

  <JsxParser
    bindings={{
      thing1: { attr1: 'One' },
      thing2: { attr2: 'Two },
    }}
    components={{ MyComponent }}
    jsx={`
      <MyComponent data={{ ...thing1, ...thing2 }}>((data) => "...")</MyComponent>
    `}
  />

This PR updates the logic related to ObjectExpression and SpreadElement to offer limited support for additional spread functionality. I realize a change of this nature could open a can of worms, so I tried to be careful by making sure not to allow any function definitions through, but I'm new to acorn and this library so lmk if I missed anything.

For additional context on my use case, the app I'm building allows users to configure views (via react-jsx-parser) as well as supply data to the components via async requests (which they can also configure). The users have control over the data going into the components, but not the components available in the parser context, so I'd like to offer them more flexibility (a la spreading within react-jsx-parser) to allow them to shape their data according to the component contracts, and to avoid more complicated workarounds. That said, I recognize the potential risk for this change and am open to any feedback / alternatives. Thank you!

andrewkshim avatar Jun 11 '22 02:06 andrewkshim