Set State
Set State
~~~~~~~~~
this.setState(
{propertyName1: propertyValue1},
{propertyName2: propertyValue2}
// and many more...
);
3. Input Element
In React, the Input Element value can be handled in two ways:
Controlled Input
Uncontrolled Input
3.1 Controlled Input
If the Input Element value is handled by a React State then it is called Controlled
Input. Controlled Inputs are the React Suggested way to handle Input Element value.
render() {
const {searchInput} = this.state
return (
<input
type="text"
onChange={this.onChangeSearchInput}
value={searchInput}
/>
)
}
}
-----------------------------------------------------------------------------------
-----------------------------
3.2 Uncontrolled Input
If the Input Element value is handled by the browser itself then it is called
Uncontrolled Input.
Uncontrolled inputs are like traditional HTML form inputs. Its value can only be
set by a user, but not programmatically. However, in controlled input value is
programmatically handled using React State.
import './App.css'
const initialUserDetailsList = [
{
uniqueNo: 1,
imageUrl: 'https://assets.ccbp.in/frontend/react-js/esther-howard-img.png',
name: 'Esther Howard',
role: 'Software Developer'
},
{
uniqueNo: 2,
imageUrl: 'https://assets.ccbp.in/frontend/react-js/floyd-miles-img.png',
name: 'Floyd Miles',
role: 'Software Developer'
},
{
uniqueNo: 3,
imageUrl: 'https://assets.ccbp.in/frontend/react-js/jacob-jones-img.png',
name: 'Jacob Jones',
role: 'Software Developer'
},
{
uniqueNo: 4,
imageUrl: 'https://assets.ccbp.in/frontend/react-js/devon-lane-img.png',
name: 'Devon Lane',
role: 'Software Developer'
}
]
render() {
const {searchInput, usersDetailsList} = this.state
const searchResults = usersDetailsList.filter(eachUser =>
eachUser.name.includes(searchInput)
)
return (
<div className="app-container">
<h1 className="title">Users List</h1>
<input
type="search"
onChange={this.onChangeSearchInput}
value={searchInput}
/>
<ul className="list-container">
{searchResults.map(eachUser => (
<UserProfile
userDetails={eachUser}
key={eachUser.uniqueNo}
deleteUser={this.deleteUser}
/>
))}
</ul>
</div>
)
}
}
import './index.css'
###################################################################################
##############################