Assignment 5
Assignment 5
In React.js, you can handle changes to a component's property using lifecycle methods or React
hooks. The specific approach depends on whether you are working with class components or
functional components.
For Class Components:
componentWillReceiveProps(nextProps): This lifecycle method is called when the component
receives new properties.
componentWillReceiveProps(nextProps) {
if (this.props.someProp !== nextProps.someProp) {
// Handle the change in properties here
}
}
componentDidUpdate(prevProps, prevState): This lifecycle method is called after the component
updates and receives new properties or state
componentDidUpdate(prevProps) {
if (this.props.someProp !== prevProps.someProp) {
// Handle the change in properties here
}
}
useEffect(() => {
// Handle the change in properties here
}, [someProp]);
//MAP FUNCTION
const square=(n1)=>{
return n1*n1;
}
const arr=[1,2,3,4,5,6,7,8,9,10];
const squaredArr= arr.map(square); //map is higher order func and sqaure is
callback passed ass arg
console.log(squaredArr);
const cube=(n1)=>{
return n1*n1*n1;
}
const array=[1,2,3,4,5];
const cubedArr=arr.map(cube);
console.log(cubedArr);
//FILTER FUNCTION
const filterodd=(num)=>{
if(num%2==0){
return true
}
else{
return false
}
}
const arr=[1,2,3,4,5,6,7,8,9,10];
const filteredArr= arr.filter(filterodd);//parameter
console.log(filteredArr);
const greaterThan=(n)=>{
if(n<6){
return false
}
else{
return true
}
}
const arr3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const greaterArr = arr.filter(greaterThan);//parameter
console.log(greaterArr);