)
}
Style-binding:
Stylebinding is a technique where the styles are dynamically configured for an element
In JS
Welcome
we need to dynamically change the appearance
document.querySelector("h1").style.textAlign="center"
in dynamic scenarios we use camel casing
text-align=>textAlign
background-color=backgroundColor
font-size=fontSize
color=>color
//perfectly valid in HTML but invalid in react
//valid on react
example
Abhi Shopping Mall
style={{ color: 'red', textAlign: 'center' }}
style object contains key and value
key is style prop defined in camelCase
value is defined in string format
Real time example of style binding
import { useState } from "react"
export function StyleBinding() {
const [textColor, setTextColor] = useState({ color: 'black' })
function ChangeColor(ourColor) {
setTextColor({ color: ourColor })
}
function ResetColor() {
setTextColor({ color: 'black' })
}
return (
Style Binding
ChangeColor('red')} onMouseOut={ResetColor}>
ChangeColor('blue')} onMouseOut={ResetColor}>
ChangeColor('green')} onMouseOut={ResetColor}>
Demo Text
)
}
React Class Binding:
Class binding is a technique used to configure CSS classes to elements dynamically
jsx cant use class attribute to bind css classes
jsx require className prop to bind a CSS class
syntax:
ex:
import { useState } from "react"
export function ClassBinding() {
const [theme, setTheme] = useState('')
const [btnTheme, setBtnTheme] = useState('btn btn-dark w-100')
function SwitchTheme(e) {
if (e.target.checked) {
setTheme('border border-1 p-4 bg-dark text-white')
setBtnTheme('btn btn-light w-100')
}
else {
setTheme('border border-1 p-4')
setBtnTheme('btn btn-dark w-100')
}
}
return (
)
}
Now we will see some keyboard events
onKeyUp
onKeyDown
onKeyPress
ex in HTML
Document
Username:
if u use keypress instead of onkeyup in the above example keypress works only when u key in next character which is not suitable for our scenario
keypress is used when u want to read ascii values or character specific things but react community doesn't recommend it.
we can use instead:
string properties
charAt
charCodeAt(0)
or onkeychange
keyup and keydown are the events used with elements when u are verifying input chars
keypress is used with elements when u are verifying the ASCII code of input chars
keypress fires up when u finish current key and start keying in another key.
ex:
import axios from "axios"
import { useEffect, useState } from "react"
export function KeyDemo() {
const [users, setUsers] = useState([{ UserId: '' }])
const [userError, setUserError] = useState('')
const [userClass, setUserClass] = useState('')
useEffect(() => {
axios.get('user.json')
.then(response => {
setUsers(response.data)
})
})
function VerifyUser(e) {
var userId = e.target.value
for (var user of users) {
if (user.UserId == userId) {
setUserError("User Id already taken")
setUserClass("text-danger")
break;
}
else {
setUserError("User id is availaible")
setUserClass("text-success")
}
}
}
return (
Register user
Username
{userError}
Password
)
}
extended example
import axios from "axios"
import { useEffect, useState } from "react"
export function KeyDemo() {
const [users, setUsers] = useState([{ UserId: '' }])
const [userError, setUserError] = useState('')
const [userClass, setUserClass] = useState('')
const [pwd, setPwd] = useState('')
useEffect(() => {
axios.get('user.json')
.then(response => {
setUsers(response.data)
})
})
function VerifyUser(e) {
var userId = e.target.value
for (var user of users) {
if (user.UserId == userId) {
setUserError("User Id already taken")
setUserClass("text-danger")
break;
}
else {
setUserError("User id is availaible")
setUserClass("text-success")
}
}
}
function VerifyPassword(e) {
if (e.target.value.match(/(?=.*[A-Z])\w{4,15}/)) {
setPwd("Strong Password")
setUserClass("text-success")
}
else {
if (e.target.value.length < 4) {
setPwd("Poor Password")
setUserClass("text-danger")
}
else {
setPwd("Weak password")
setUserClass("text-warning")
}
}
}
return (
Register user
Username
{userError}
Password
{pwd}
)
}
ex:
import axios from "axios"
import { useEffect, useState } from "react"
export function KeyDemo() {
const [users, setUsers] = useState([{ UserId: '' }])
const [userError, setUserError] = useState('')
const [userClass, setUserClass] = useState('')
const [pwd, setPwd] = useState('')
const [pwdStrength, setPwdStrength] = useState({ width: '' })
const [bgClass, setBgClass] = useState("")
useEffect(() => {
axios.get('user.json')
.then(response => {
setUsers(response.data)
})
})
function VerifyUser(e) {
var userId = e.target.value
for (var user of users) {
if (user.UserId == userId) {
setUserError("User Id already taken")
setUserClass("text-danger")
break;
}
else {
setUserError("User id is availaible")
setUserClass("text-success")
}
}
}
function VerifyPassword(e) {
if (e.target.value.match(/(?=.*[A-Z])\w{4,15}/)) {
setPwd("Strong Password")
setUserClass("text-success")
setPwdStrength({ width: '100%' })
setBgClass("bg-success")
}
else {
if (e.target.value.length < 4) {
setPwd("Poor Password")
setUserClass("text-danger")
setPwdStrength({ width: '45%' })
setBgClass("bg-danger")
}
else {
setPwd("Weak password")
setUserClass("text-warning")
setPwdStrength({ width: '55%' })
setBgClass("bg-warning")
}
}
}
return (
Register user
Username
{userError}
Password
{/*
*/}
{pwd}
{/*
{pwd}
*/}
)
}
While working with keyboard events what are the arguments we can use:
-keycode:it returns actual ASCII code of key
-charCode: it returns code of char according to UTF
-which: similar to keycode but compatible with various layout
-shiftKey: retuns Boolean true or false
-ctrlKey: same
-altKey:same
import axios from "axios"
import { useEffect, useState } from "react"
export function KeyDemo() {
const [codeError, setCodeError] = useState('d-none')
function VerifyCode(e) {
if (e.which >= 65 && e.which <= 90) {
setCodeError('d-block')
} else {
setCodeError('d-none')
}
}
return (
Verify Code
Warning: Caps is on
)
}
task: to try apart from event such as keypress
Element State events:
onChange: action when value changes
onBlur: Action when element lost focus
onFocus: action when element got focus
import { useState } from "react"
export function StateEvent() {
const [msg, setMsg] = useState('')
function HandleFocus() {
setMsg("IFSC Code: SBIN0001498,HDFC00004356")
}
function HandleBlur() {
setMsg('')
}
return (
State Events
Bank IFSC Code
{msg}
)
}
example:
import { useState } from "react"
export function StateEvent() {
const [msg, setMsg] = useState('')
const [code, setCode] = useState('')
function HandleFocus() {
setMsg("IFSC Code: SBIN0001498,HDFC00004356")
}
function HandleBlur() {
setMsg('')
}
function HandleChange(e) {
setCode(e.target.value.toUpperCase())
}
return (
State Events
Bank IFSC Code
{msg}
)
}
Button events
-onClick()
-onDoubleClick() in JS its ondblclick
-onContextMenu right click
-onSelectStart in JS react will currently not support it
ex:
export function ButtonEvent() {
function HandleDbClick() {
window.open('iphone15.png', 'Iphone', 'width=300 height=300')
}
function HandleContextMenu() {
document.oncontextmenu = function () {
alert("right click is disabled")
return false
}
}
return (