08-State,useEffect,String binding
08-State,useEffect,String binding
Variables are immutable and component can't handle data with immutable
reference.
State in Component
a) useState()
b) useContext()
c) useRef()
d) useMemo()
e) useCallBack()
f) useReducer()
etc..
- State is mutable.
- The local state is defined by using "useState()" hook.
- Local state is configured while creating the component and destroyed when user
navigates to another component.
Syntax:
import { useState } from "react";
FAQ's:
2. If you configure state using const how you will assign a new value?
A. const will not allow assignment, but allows changing the value in memory by re-
initializing the memory. Setter re-initializes the memory and stores a new value
into state.
Syntax:
var [name, setName] = useState();
3. When to setState ?
A. State can't set values while creating a component. You have to configure values
into state after component is created, which can be "mount" or any element
event.
Syntax:
useEffect(()=>{
}, [ dependencies ]);
useEffect(function(){ }, [ ]);
Every component mounts only once, you have to define the dependency to
mount again. You can configure multiple dependencies for a component.
Ex:
data-binding.jsx
useEffect(()=>{
setPrice(70000.44);
},[])
return(
<div>
<h2>Data Binding</h2>
Price : { price }
</div>
)
}
Syntax:
`string ${expression}`
Ex:
data-binding.jsx
useEffect(()=>{
setProductName("Samsung TV");
setBgStyle('bg-warning');
}, []);
return(
<div className="container-fluid">
<h2 className={`text-center text-danger ${bgStyle}`} >Data Binding</h2>
<dl>
<dt>Name</dt>
<dd>{productName}</dd>
</dl>
</div>
)
}
Formatting:
bold(), italics(), fontsize(), fontcolor(), toUpperCase(), toLowerCase()
etc..
Manipulation:
length, indexOf(), lastIndexOf(), charAt(), charCodeAt(), match(), slice(),
substring(), substr(), etc..