React: Simple Event Emitter on form example

Clash 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>beginner form react.js jsx
add a comment |Â
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>beginner form react.js jsx
add a comment |Â
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>beginner form react.js jsx
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>beginner form react.js jsx
asked May 25 at 17:46
Kate Herasimenak
3016
3016
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password