r/reactjs • • Jul 28 '19

Show /r/reactjs ScriptUI: Write React without JSX

https://github.com/anater/ScriptUI
3 Upvotes

26 comments sorted by

View all comments

4

u/[deleted] Jul 28 '19

I'm really not following how this would work for a complex example.

Can you translate this ContactUs form into ScriptUI so I can see how props are supposed to be passed down?

function ContactUs(props) {

  const handleChange = (e) => { /* setState({ [e.target.name]: [e.target.value] }) */};

  return (
    <form>
      <div>
        <label>
          Enter your name
          <input type="text" name="contactedBy" onChange={handleChange} />
        </label>
      </div>
      <div>
        <label>
          Enter your message
          <input type="text" name="message" onChange={handleChange} />
        </label>
      </div>
    </form>
  )
}

3

u/[deleted] Jul 28 '19

Sure, I'd do something like this:

const Form = View("form");
const Field = (label, props) => {
  return View("div)(
    View("label")(
      label, 
      View("input, { type: "text", ...props })()
  )
};

function ContactUs(props) {
  const handleChange = (e) => { };
  
  return Form(
    Field("Enter your name")
      .set({ name: "contactedBy", onChange: handleChange }),
    Field("Enter your message")
      .set({ name: "message", onChange: handleChange })
  );
}

1

u/[deleted] Jul 28 '19

Ok, looks like I was close, but this is obviously a lot cleaner than my first attempt. Thanks!

1

u/[deleted] Jul 28 '19

You’re welcome! Thank you for checking it out and caring enough to ask! I’ll work on adding more examples so this is clearer for the next dev