Unit - II
Unit - II
● If-else statement
An if…else statement will execute the actions contained in the if block when the
condition is satisfied. Otherwise, it will execute the actions contained in the else block.
In JSX, you are able to use JavaScript code with markup to render dynamic values within
your application. JSX uses curly braces ({ and }) to signify expressions that need to be
interpreted prior to rendering. The caveat, however, is that there is a limit to what can be
done within such braces.
Example
function App() {
let a=30;
let msg="";
if(a>=10)
else
return (
<>
<p>{msg}</p>
</>
);
If the condition is true,the expression after the logical && operator will be the output. If
the condition is false,React ignores and skips the expression.
If the condition is true, the element right after && will appear in the output.If it is false ,
React will ignore and skip it. Note that returning a falsy expression will still cause the
element after && to be skipped but will return the falsy expression.
true && expression always evaluates to expression, and false && expression always
evaluates to false.
function App() {
let a=40;
let b=10;
let c=100;
let msg="";
else
return (
<>
<p>{msg}</p>
</>
);
Operators
switch case operator to handle the conditional rendering of these multiple states.
The switch statement in JavaScript is used to evaluate an expression and execute the
corresponding code block based on the expression’s value. In React, we can use switch
statements inside a component to conditionally render different elements or components
based on a particular value.
switch statements can be used inside a component to render different content based on a
particular condition.
function App() {
let a=40;
let b=10;
let addition="addition";
let sub="substraction";
let ch="addition";
let action=0;
switch(ch)
case "addition" :
action=parseInt(a)+parseInt(b);
break;
case "sub" :
action=parseInt(a)-parseInt(b);
break;
default:
return (
<>
<h4>{addition}</h4>
<h4>{sub}</h4>
<h5>{ch}={action}</h5>
</>
);
The key prop is a unique identifier that React uses to efficiently manage and update
elements within a list. When rendering a list of elements, it is crucial to assign a unique
key to each element to help React differentiate between the items and perform updates
more efficiently.React's key prop gives you the ability to control component instances.
If a unique value is required, we need to pass keys as a prop with a distinct prop name.
function App(props) {
return (
<>
<h3>Your name is {props.name}</h3>
</>
);
index.js file
root.render(
<React.StrictMode>
<App name="raju"/>
</React.StrictMode>
);
JavaScript array map() method is used to iterate through the data in the form of an array.
This map() in react is used to create the list by iterating the data and transforming it into
the list components using HTML tags.
● References
ReactJS Refs are used to access and modify the DOM elements in the React Application.
It creates a reference to the elements and uses it to modify them.
Short for “reference”, refs are a way to access underlying DOM elements in a React
component.
Refs is the shorthand used for references in React. It is similar to keys in React. It is an
attribute which makes it possible to store a reference to particular DOM nodes or React
elements. It provides a way to access React DOM nodes or React elements and how to
interact with it. It is used when we want to change the value of a child component,
without making the use of props.
● useRef
It can be used to store a mutable value that does not cause a re-render when updated.
● Create Refs
Refs are created using React.createRef() and attached to React elements via the ref
attribute. Refs are commonly assigned to an instance property when a component is
constructed so they can be referenced throughout the component.
● Access Refs
In React, when a ref is passed to an element in render using the ref attribute, the
underlying DOM element or React component becomes accessible at the current property
of the ref.
Refs are a function provided by React to access the DOM element and the React elements
created in components. They are used in cases where we want to change the value of a
child component, without making use of props and state.
The bind() is an inbuilt method in React that is used to pass the data as an
argument to the function of a class based component.
Syntax: this.Function.
The simplest use of bind() is to make a function that, no matter how it is called, is
called with a particular this value
● Arrow Function
The arrow function is a new feature of ES6, introduced in ReactJS 16. It allows the
developer to create a function that has lexical “this” binding and no arguments.
Arrow functions offer a compressed and short version of a function expression and need
fewer keystrokes than regular JavaScript functions from the developer and can be used as
a simpler alternative to functions within class components and functional components
and event handlers in React.
Arrow functions are always anonymous, meaning there is no need to use the keyword
“function” when defining them. They also do not have their own this value, meaning that
this inside an arrow function will refer to the one where it was created rather than where
it was called from.
The third difference between regular JavaScript functions and arrow functions is that all
arguments passed into an arrow function must be pre−defined, because there is no need
for them to be assigned as default values like with regular JavaScript functions.
It allows you to create functions in a cleaner way. The arrow function is a compressed
and shorter version of the function expression. It requires fewer keystrokes by
developers.
What is a state
The state is a built-in React object that is used to contain data or
information about the component. A component's state can change
over time; whenever it changes, the component re-renders.
React components has a built-in state object. The state object is where you store
property values that belong to the component. When the state object changes, the
component re-renders.
React's state is great for handling data that's specific to a single component. It's simple
and lightweight, making it suitable for components that don't need to share data widely.
You can modify state using the setState function.
What is Props
In ReactJS, the props are a type of object where the value of
attributes of a tag is stored. The word “props” implies “properties”, and
its working functionality is quite similar to HTML attributes. Basically,
these props components are read-only components.
props validation
Props are an important mechanism for passing the read-only attributes to React
components. The props are usually required to use correctly in the component. If it is not
used correctly, the components may not behave as expected. Hence, it is required to use
props validation in improving react components.
Props validation is a tool that will help the developers to avoid future bugs and problems.
It is a useful way to force the correct usage of your components. It makes your code more
readable. React components used special property PropTypes that help you to catch bugs
by validating data types of values passed through props, although it is not necessary to
define components with propTypes. However, if you use propTypes with your
components, it helps you to avoid unexpected bugs.
Validating Props
App.propTypes is used for props validation in react component. When some of the props
are passed with an invalid type, you will get the warnings on JavaScript console. After
specifying the validation patterns, you will set the App.defaultProps.
Syntax:
class App extends React.Component {
render() {}
}
Component.propTypes = { /*Definition */};
React components have their own local state that can be managed independently. State is
initialized using the useState hook and can be updated using the setState function.
Components can access and modify their local state directly. To initialize state, we can
use the useState hook and provide an initial value.
The local state can be managed using React's setState() method. Context: React's
context API allows you to pass data through a component tree without having to pass
props down manually at every level.
React state management is a process for managing the data that React components need in order
to render themselves.