Lists and Forms Updated: 03 September 2023
Based on this EdX course
Lists
We can render a list of react elements using a few different methods
Loop
for ( let i = 0 , i < array.length ; i ++ ) {
elements . push ( < li > { array [ i ] } </ li > )
document . getElementById ( " root " )
Map
{ name : ' John ' , age : 5 },
{ name : ' Jeff ' , age : 6 },
{ name : ' Jenny ' , age : 7 },
var elements = array . map ( ( el ) => (
Name: { el . name } , Age: { el . age }
ReactDOM . render ( < ol > { elements } </ ol > , document . getElementById ( ' root ' ))
Map in JSX
{ name : ' John ' , age : 5 },
{ name : ' Jeff ' , age : 6 },
{ name : ' Jenny ' , age : 7 },
Name: { el . name } , Age: { el . age }
document . getElementById ( ' root ' )
Using Keys
We can also use unique keys to render items quicker with the following
{ id : 1 , name : ' John ' , age : 5 },
{ id : 2 , name : ' Jeff ' , age : 6 },
{ id : 3 , name : ' Jenny ' , age : 7 },
Name: { el . name } , Age: { el . age }
document . getElementById ( ' root ' )
{ id : 1 , name : ' John ' , age : 5 },
{ id : 2 , name : ' Jeff ' , age : 6 },
{ id : 3 , name : ' Jenny ' , age : 7 },
{ array . map ( ( el , index ) => (
Name: { el . name } , Age: { el . age }
document . getElementById ( ' root ' )
List Component
It can be useful to define a component that specifically renders lists as follows
class ListItem extends React . Component {
< li key = { this . props . index } >
Name: { this . props . name } , Age: { this . props . age }
class List extends React . Component {
{ this . props . list . map ( ( el , index ) => (
< ListItem index = { index } name = { el . name } age = { el . age } />
{ id : 1 , name : ' John ' , age : 5 },
{ id : 2 , name : ' Jeff ' , age : 6 },
{ id : 3 , name : ' Jenny ' , age : 7 },
ReactDOM . render ( < List list = { array } /> , document . getElementById ( ' root ' ))
Controlled Components
HTML form elements can be modified from the DOM as well as from the code, we use React to manage the state of these with what’s called Controlled Components
We tie the DOM state to the React state in order to more easily manage it
This is done with the following steps
When an input value is changed, call an event handler to update the value
Re-render the element with its new value
class ControlledText extends React . Component {
this . state = { value : '' }
this . handleChange = this . handleChange . bind ( this )
this . setState ( { value : event . target . value } )
onChange = { this . handleChange }
Checkboxes
class ControlledCheckbox extends React . Component {
this . state = { value : '' }
this . handleChange = this . handleChange . bind ( this )
this . setState ( { value : event . target . checked } )
checked = { this.state. checked }
onChange = { this. handleChange }
Text Areas
class ControlledText extends React . Component {
this . state = { value : '' }
this . handleChange = this . handleChange . bind ( this )
this . setState ( { value : event . target . value } )
onChange = { this . handleChange }
Selects
class ControlledSelect extends React . Component {
this . state = { value : 0 }
this . setState ( { value : even . target . value } )
< select value = { this . state . value } onChange = { this . handleChange } >
< option value = " 0 " > Please select an option </ option >
< option value = " 1 " > One </ option >
< option value = " 2 " > Two </ option >
< option value = " 3 " > Three </ option >
Selects can also be dynamically generated as follows
class ControlledSelect extends React . Component {
this . state = { value : 0 }
this . setState ( { value : even . target . value } )
var options = [ ' Please select an option ' , ' One ' , ' Two ' , ' Three ' ]
< select value = { this . state . value } onChange = { this . handleChange } >
options.map((option, index) => < option value = { index } > { option } </ option > )
class ControlledMultiple extends React . Component {
this . state = { value : ' apple ' }
this . handleChange = this . handleChange . bind ( this )
this . setState ( { [ event . target . name ] : event . target . value } )
var array = [ ' apple ' , ' banana ' , ' carrot ' , ' donuts ' ]
var options = array . map ( ( item ) => < option value = { item } > { item } </ option > )
value = { this . state . inputName }
onChange = { this . handleChange }
value = { this . state . textAreaName }
onChange = { this . handleChange }
value = { this . state . selectName }
onChange = { this . handleChange }
Tutorial
The Codepen for the Tutorial can be found here