Skip to content

Instantly share code, notes, and snippets.

@SanderSpies
Last active March 30, 2016 07:26
Show Gist options
  • Save SanderSpies/b793bcb065dd720a9072c696606b8aae to your computer and use it in GitHub Desktop.
Save SanderSpies/b793bcb065dd720a9072c696606b8aae to your computer and use it in GitHub Desktop.
React pattern matching braindumb
const test = (props) => (
<div>
{match(props).with(
(String) => <div>Should work {props}</div>,
({bar = '1', test: {x = false}}) => <div>Super cool {bar}</div>,
({bar = '5', test: {y = true}}) => <div>Mega cool {test.y} {y}</div>,
([a = 3, {x = 4}, z]) =>
<nesting>
{match(z).with(
({bar = '5', test: {y = true}}) =>
<div>
Foobar {a} {test.x} {z}
</div>
)}
</nesting>
)}
</div>
);
@SanderSpies
Copy link
Author

This should translate into something like:

function matchPattern1(props) {
   // combine checks here for this
}
function matchPattern2(props) {
   // etc. etc.
}
<div>
{ matchPattern1(props) ?
  <div />
: matchPattern2(props) ?
  <div /> 
: matchPattern3(props) ?
  (function(){
    return <div />;
  })()
: null
}
</div>

@SanderSpies
Copy link
Author

RegExp solution is ugly, not sure yet how to improve.

@SanderSpies
Copy link
Author

This should also be supported:

{foo = String, bar = Boolean})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment