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>
  )
}

6

u/[deleted] Jul 28 '19

This is as far as I got before getting utterly lost in HOCs:

function ContactUs(props) {

  const handleChange = (e) => {}

  return View('form')
    ([View('div')
      (View('label')
        (
          ['Enter your name',
          View('input', { type:'text', name:'contactedBy', onChange:handleChange })]
        )
      ),
      View('div')
        (View('label')
          (
            ['Enter your name'],
            View('input', {type:'text', name:'message', onChange:handleChange })
          )
        )
    ])
}

4

u/[deleted] Jul 28 '19

Yes, this is right. Since its all JS functions, it enables more compositional style. So using HOCs is encouraged. Using deeply nested structures with the `View` primitive is only slightly more usable than `React.createElement` or `h`.