React: Simple Event Emitter on form example

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I decided to study EventEmitter and play around with it in React JS for on example of a simple form.

I built EventEmitter and added event 'newdata' to it. On event 'newdata' is called function where the data for form's rendering changes. Then event 'newdata' is emitted with the new data.
I repeated emitting of event using setTimeout to better understand how this works.



I will be happy for the tip on improving the code.






class EventEmitter 
constructor()
this.events =


on(eventName, fn)
if(!this.events[eventName])
this.events[eventName] =


this.events[eventName].push(fn)
return () =>
this.events[eventName] = this.events[eventName].filter(eventFn => fn !== eventFn)



emit(eventName, data)
const event = this.events[eventName]
if (event)
event.forEach(fn =>
fn.call(null, data)
)




let emitter = new EventEmitter()

class App extends React.Component
constructor(props)
super(props)

this.state =
firstName: '',
isValid: false,



handleSubmit(e)
e.preventDefault()

let firstName, isValid

emitter.on('newdata', function(data)
firstName = data['firstName']
isValid = data['isValid']
)

emitter.emit('newdata',
firstName: 'Kitty',
isValid: true,
)

this.setState(firstName, isValid)

if (isValid)
console.log('success')
else
console.log('error')


setTimeout(() =>

emitter.emit('newdata',
firstName: 'Joni',
isValid: false,
)

this.setState(firstName, isValid)

if (isValid)
console.log('success')
else
console.log('error')


, 1000);


handleValidation(e)
this.setState(
firstName: e.target.firstName
)


render()
return (
<form>
<label>
Name:
</label>
<input
type='text'
name ='firstName'
value = this.state.firstName
onChange = this.handleValidation.bind(this)
/>
<button onClick = this.handleSubmit.bind(this)>
Submit
</button>
</form>
)



ReactDOM.render(
<App />,
document.getElementById('root')
)

<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>









share|improve this question

























    up vote
    1
    down vote

    favorite












    I decided to study EventEmitter and play around with it in React JS for on example of a simple form.

    I built EventEmitter and added event 'newdata' to it. On event 'newdata' is called function where the data for form's rendering changes. Then event 'newdata' is emitted with the new data.
    I repeated emitting of event using setTimeout to better understand how this works.



    I will be happy for the tip on improving the code.






    class EventEmitter 
    constructor()
    this.events =


    on(eventName, fn)
    if(!this.events[eventName])
    this.events[eventName] =


    this.events[eventName].push(fn)
    return () =>
    this.events[eventName] = this.events[eventName].filter(eventFn => fn !== eventFn)



    emit(eventName, data)
    const event = this.events[eventName]
    if (event)
    event.forEach(fn =>
    fn.call(null, data)
    )




    let emitter = new EventEmitter()

    class App extends React.Component
    constructor(props)
    super(props)

    this.state =
    firstName: '',
    isValid: false,



    handleSubmit(e)
    e.preventDefault()

    let firstName, isValid

    emitter.on('newdata', function(data)
    firstName = data['firstName']
    isValid = data['isValid']
    )

    emitter.emit('newdata',
    firstName: 'Kitty',
    isValid: true,
    )

    this.setState(firstName, isValid)

    if (isValid)
    console.log('success')
    else
    console.log('error')


    setTimeout(() =>

    emitter.emit('newdata',
    firstName: 'Joni',
    isValid: false,
    )

    this.setState(firstName, isValid)

    if (isValid)
    console.log('success')
    else
    console.log('error')


    , 1000);


    handleValidation(e)
    this.setState(
    firstName: e.target.firstName
    )


    render()
    return (
    <form>
    <label>
    Name:
    </label>
    <input
    type='text'
    name ='firstName'
    value = this.state.firstName
    onChange = this.handleValidation.bind(this)
    />
    <button onClick = this.handleSubmit.bind(this)>
    Submit
    </button>
    </form>
    )



    ReactDOM.render(
    <App />,
    document.getElementById('root')
    )

    <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    <div id="root"></div>









    share|improve this question





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I decided to study EventEmitter and play around with it in React JS for on example of a simple form.

      I built EventEmitter and added event 'newdata' to it. On event 'newdata' is called function where the data for form's rendering changes. Then event 'newdata' is emitted with the new data.
      I repeated emitting of event using setTimeout to better understand how this works.



      I will be happy for the tip on improving the code.






      class EventEmitter 
      constructor()
      this.events =


      on(eventName, fn)
      if(!this.events[eventName])
      this.events[eventName] =


      this.events[eventName].push(fn)
      return () =>
      this.events[eventName] = this.events[eventName].filter(eventFn => fn !== eventFn)



      emit(eventName, data)
      const event = this.events[eventName]
      if (event)
      event.forEach(fn =>
      fn.call(null, data)
      )




      let emitter = new EventEmitter()

      class App extends React.Component
      constructor(props)
      super(props)

      this.state =
      firstName: '',
      isValid: false,



      handleSubmit(e)
      e.preventDefault()

      let firstName, isValid

      emitter.on('newdata', function(data)
      firstName = data['firstName']
      isValid = data['isValid']
      )

      emitter.emit('newdata',
      firstName: 'Kitty',
      isValid: true,
      )

      this.setState(firstName, isValid)

      if (isValid)
      console.log('success')
      else
      console.log('error')


      setTimeout(() =>

      emitter.emit('newdata',
      firstName: 'Joni',
      isValid: false,
      )

      this.setState(firstName, isValid)

      if (isValid)
      console.log('success')
      else
      console.log('error')


      , 1000);


      handleValidation(e)
      this.setState(
      firstName: e.target.firstName
      )


      render()
      return (
      <form>
      <label>
      Name:
      </label>
      <input
      type='text'
      name ='firstName'
      value = this.state.firstName
      onChange = this.handleValidation.bind(this)
      />
      <button onClick = this.handleSubmit.bind(this)>
      Submit
      </button>
      </form>
      )



      ReactDOM.render(
      <App />,
      document.getElementById('root')
      )

      <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
      <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
      <div id="root"></div>









      share|improve this question











      I decided to study EventEmitter and play around with it in React JS for on example of a simple form.

      I built EventEmitter and added event 'newdata' to it. On event 'newdata' is called function where the data for form's rendering changes. Then event 'newdata' is emitted with the new data.
      I repeated emitting of event using setTimeout to better understand how this works.



      I will be happy for the tip on improving the code.






      class EventEmitter 
      constructor()
      this.events =


      on(eventName, fn)
      if(!this.events[eventName])
      this.events[eventName] =


      this.events[eventName].push(fn)
      return () =>
      this.events[eventName] = this.events[eventName].filter(eventFn => fn !== eventFn)



      emit(eventName, data)
      const event = this.events[eventName]
      if (event)
      event.forEach(fn =>
      fn.call(null, data)
      )




      let emitter = new EventEmitter()

      class App extends React.Component
      constructor(props)
      super(props)

      this.state =
      firstName: '',
      isValid: false,



      handleSubmit(e)
      e.preventDefault()

      let firstName, isValid

      emitter.on('newdata', function(data)
      firstName = data['firstName']
      isValid = data['isValid']
      )

      emitter.emit('newdata',
      firstName: 'Kitty',
      isValid: true,
      )

      this.setState(firstName, isValid)

      if (isValid)
      console.log('success')
      else
      console.log('error')


      setTimeout(() =>

      emitter.emit('newdata',
      firstName: 'Joni',
      isValid: false,
      )

      this.setState(firstName, isValid)

      if (isValid)
      console.log('success')
      else
      console.log('error')


      , 1000);


      handleValidation(e)
      this.setState(
      firstName: e.target.firstName
      )


      render()
      return (
      <form>
      <label>
      Name:
      </label>
      <input
      type='text'
      name ='firstName'
      value = this.state.firstName
      onChange = this.handleValidation.bind(this)
      />
      <button onClick = this.handleSubmit.bind(this)>
      Submit
      </button>
      </form>
      )



      ReactDOM.render(
      <App />,
      document.getElementById('root')
      )

      <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
      <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
      <div id="root"></div>








      class EventEmitter 
      constructor()
      this.events =


      on(eventName, fn)
      if(!this.events[eventName])
      this.events[eventName] =


      this.events[eventName].push(fn)
      return () =>
      this.events[eventName] = this.events[eventName].filter(eventFn => fn !== eventFn)



      emit(eventName, data)
      const event = this.events[eventName]
      if (event)
      event.forEach(fn =>
      fn.call(null, data)
      )




      let emitter = new EventEmitter()

      class App extends React.Component
      constructor(props)
      super(props)

      this.state =
      firstName: '',
      isValid: false,



      handleSubmit(e)
      e.preventDefault()

      let firstName, isValid

      emitter.on('newdata', function(data)
      firstName = data['firstName']
      isValid = data['isValid']
      )

      emitter.emit('newdata',
      firstName: 'Kitty',
      isValid: true,
      )

      this.setState(firstName, isValid)

      if (isValid)
      console.log('success')
      else
      console.log('error')


      setTimeout(() =>

      emitter.emit('newdata',
      firstName: 'Joni',
      isValid: false,
      )

      this.setState(firstName, isValid)

      if (isValid)
      console.log('success')
      else
      console.log('error')


      , 1000);


      handleValidation(e)
      this.setState(
      firstName: e.target.firstName
      )


      render()
      return (
      <form>
      <label>
      Name:
      </label>
      <input
      type='text'
      name ='firstName'
      value = this.state.firstName
      onChange = this.handleValidation.bind(this)
      />
      <button onClick = this.handleSubmit.bind(this)>
      Submit
      </button>
      </form>
      )



      ReactDOM.render(
      <App />,
      document.getElementById('root')
      )

      <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
      <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
      <div id="root"></div>





      class EventEmitter 
      constructor()
      this.events =


      on(eventName, fn)
      if(!this.events[eventName])
      this.events[eventName] =


      this.events[eventName].push(fn)
      return () =>
      this.events[eventName] = this.events[eventName].filter(eventFn => fn !== eventFn)



      emit(eventName, data)
      const event = this.events[eventName]
      if (event)
      event.forEach(fn =>
      fn.call(null, data)
      )




      let emitter = new EventEmitter()

      class App extends React.Component
      constructor(props)
      super(props)

      this.state =
      firstName: '',
      isValid: false,



      handleSubmit(e)
      e.preventDefault()

      let firstName, isValid

      emitter.on('newdata', function(data)
      firstName = data['firstName']
      isValid = data['isValid']
      )

      emitter.emit('newdata',
      firstName: 'Kitty',
      isValid: true,
      )

      this.setState(firstName, isValid)

      if (isValid)
      console.log('success')
      else
      console.log('error')


      setTimeout(() =>

      emitter.emit('newdata',
      firstName: 'Joni',
      isValid: false,
      )

      this.setState(firstName, isValid)

      if (isValid)
      console.log('success')
      else
      console.log('error')


      , 1000);


      handleValidation(e)
      this.setState(
      firstName: e.target.firstName
      )


      render()
      return (
      <form>
      <label>
      Name:
      </label>
      <input
      type='text'
      name ='firstName'
      value = this.state.firstName
      onChange = this.handleValidation.bind(this)
      />
      <button onClick = this.handleSubmit.bind(this)>
      Submit
      </button>
      </form>
      )



      ReactDOM.render(
      <App />,
      document.getElementById('root')
      )

      <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
      <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
      <div id="root"></div>








      share|improve this question










      share|improve this question




      share|improve this question









      asked May 25 at 17:46









      Kate Herasimenak

      3016




      3016

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195174%2freact-simple-event-emitter-on-form-example%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195174%2freact-simple-event-emitter-on-form-example%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods