Skip to content

Instantly share code, notes, and snippets.

View Giagnus64's full-sized avatar

Gianfranco Nuschese Giagnus64

View GitHub Profile
const button = document.querySelector("#like-btn")
button.addEventListener('click', handleClick)
function handleClick(event){
console.log("Button clicked!")
}
@Giagnus64
Giagnus64 / gist:b90d36b1d38560d2825cd80685a916b8
Last active September 17, 2019 16:48
Event Constructor
// pass in event type, options
const clickEvent = new Event("click", {"bubbles":true, "cancelable":false});
//call event on element
button.dispatchEvent(clickEvent);
//=> "Button clicked!"
@Giagnus64
Giagnus64 / gist:3c08e71c8afd329d9adc491ced0e14d8
Last active September 17, 2019 16:48
Keyboard Event Listener
// This event has different options
const kbEvent = new KeyboardEvent('keypress', {
key:"e"
shiftKey:"true"
});
//see how the event is called on document
document.dispatchEvent(kbEvent);
@Giagnus64
Giagnus64 / gist:e8fff81659485d815417909af2af61d1
Created September 17, 2019 18:02
Other Event Listener function
const colorBtnSim = document.querySelector(".simulate-color");
const divToChange = colorBtn.parentElement;
colorBtnSim.addEventListener('click', colorBtnSimClicked);
function colorBtnSimClicked(event){
const colors = ["skyblue", "green", "orange", "yellow"];
event.target.parentElement.style.backgroundColor = colors[Math.floor(Math.random()*colors.length)];
}
store.dispatch({type: 'WITHDRAW', amount: 10})
const initialState = {
balance: 0
}
function rootReducer(state = initialState, action) {
switch (action.type) {
case 'WITHDRAW':
return{...state, balance: state.balance - action.amount}
case 'DEPOSIT':
return {...state, balance: state.balance + action.amount}
import {createStore} from 'redux'
import rootReducer from './reducer.js'
const store = createStore(rootReducer)
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import rootReducer from './reducer'
const store = createStore(rootReducer)
import React, { Component } from 'react';
import { connect } from 'react-redux';
class Counter extends Component{
render(){
return(
<div>
<button onClick={this.props.deposit}>Deposit $10</button>
import React, { Component } from 'react';
class Counter extends Component{
state={
balance: 0
}
//const withdraw10 = () => {...}
//const deposit10 = () => {...}
render(){