import React, {useState} from 'react'
const ExamplePost = () => {
const [firstName, setFirstName] = useState('Muhammad')
const [lastName, setLastName] = useState('Jalloh')
const bioObj = {
dept: 'Engineering',
desc: 'Responsible for conceiving, developing, deploying and maintaining the platform.'
}
return (
<div>
<h4> User Bio Data </h4>
<p> First Name: {firstName} </p>
<p> Last Name: {lastName} <p>
<p> This user is part of the {bioObj.dept} team and is: {bioObj.desc} </p>
</div>
)
}
export default ExamplePost
{
firstName ? alert(`My name is ${firstName}`)
: alert(`I don't have a name yet.`)
}
<p>
{`${firstName} was born ${3 * 5} years ago.`}
</p>
<h3 className='text-primary'> My Title </h3>
<h3 style={{ color: 'blue'}}> My Title </h3>
import React from 'react'
const Profile = () => {
const memberStuff = [
{name: 'Books', link: '/books'},
{name: 'Journals', link: 'journals'},
{name: 'Magazines', link: 'mags'},
{name: 'Special Offers', link: 'offers'},
{name: 'Downloads', link: 'downloads'}
]
const stuffStyle = {
color: 'green',
marginTop: '15px',
// Add as many CSS styles as you want here.
}
return(
<>
<h5 style={{ margin: '25px'}}> Members-Only Stuff </h5>
<div>
These are your members-only stuff.
<ul>
{memberStuff.map(item => (
<li key={item.link} style={stuffStyle}>
<a href={`members/${item.link}`}> {item.name} </a>
</li>
))}
</ul>
</div>
</>
)
}
export default Profile
<h3 className={firstName ? 'text-success' : 'text-danger'}> My Title </h3>
<div> <h3style={{color:firstName ? 'green': 'red'}}> My Second Title </h3></div>
import React from 'react'
const ExamplePost = (props) => {
return (
<div>
<button onClick={props.doSomething}> Click Me </button>
</div>
)
}
export default ExamplePost
import React from 'react'
const ExamplePost = () => {
const doSomething = () => {
console.log(`do something here...`)
}
return (
<div>
<button onClick={doSomething}> Click! </button>
</div>
)
}
export default ExamplePost
import React, {useState} from 'react'
const ExamplePost = () => {
const [userName, setUserName] = useState('')
const [userEmail, setUserEmail] = useState('')
function submitForm(e){
e.preventDefault()
if(userName && userEmail){
// Ideally, you will make an API call to submit the form data here
// But we will settle for a console.log() and an alert()
console.log(`${userName}: ${userEmail}`)
alert(`Thanks, ${userName}`)
// Clear/reset all the form fields
setUserName('')
setUserEmail('')
}
}
return (
<div>
{/* The <form>'s onClick listener is referencing the submitForm() function defined above. */}
<form onSubmit={submitForm}>
<input type='text' value={userName} placeholder='Your User Name'
// The onChange listener is passed an inline JS function
onChange={(e) => setUserName(e.target.value)} />
<input type='email' value={userEmail}
// The onChange listener is passed an inline JS function
onChange={(e) => setUserEmail(e.target.value)}
placeholder='Your Email' />
<button type='submit'> Submit </button>
</form>
</div>
) }
export default ExamplePost
import React, {useState} from 'react'
const Profile = () => {
const [userIsLoggedIn, setUserIsLoggedIn] = useState(false)
const memberStuff = ['Books', 'Journals', 'Magazines', 'Special Offers']
const memberUI = () => (
<div>
<h5 style={{ margin: '25px'}}> Members-Only Stuff </h5>
<div>
These are your members-only stuff.
<ul>
{memberStuff.map(item => (
<li key={item}>
{item}
</li>
))}
</ul>
</div>
</div>
)
const unregisteredUI = () => {
return(
<>
<h5 style={{ margin: '25px'}}> These are our Free-for-all Stuff </h5>
<div>>
<p>
We currently don't have stuff for non-registered members.
</p>
<p>
Please, <a href='#'> register </a>>and login to access the members-only stuff.
</p>
</div>
</>
)
}
return(
<>
{userIsLoggedIn ? (
<>
{memberUI()}
</>
): (<> {unregisteredUI()} </> )}
</>
)
}
export default Profile
return(
<>
{userIsLoggedIn ? (
<>
{memberUI}
</>
): (<> {unregisteredUI} </> )}
</>
)
Warning: Functions are not valid as a React child.
{ memberUI() }
and { unregisteredUI() }
and not { memberUI }
and {unregisteredUI}
(like we did in the example above.)
import React, {useState} from 'react'
const Profile = () => {
const [userIsLoggedIn, setUserIsLoggedIn] = useState(false)
const memberStuff = [
{name: 'Books', link: 'books'},
{name: 'Journals', link: 'journals'},
{name: 'Magazines', link: 'mags'},
{name: 'Special Offers', link: 'offers'},
{name: 'Downloads', link: 'downloads'}
]
return(
<div>
{userIsLoggedIn && (
<>
<h5 style={{ margin: '25px'}}> Members-Only Stuff </h5>
<div>
These are your members-only stuff.
<ul>
{memberStuff.map(item => (
<li key={item.link}>
<a href={`members/${item.link}`}> {item.name} </a>
</li>
))}
</ul>
</div>
</>
)}
</div>
)
}
export default Profile
{userIsLoggedIn && (
<p> You are logged in. </p>
)}
{userIsLoggedIn ? (
<div>
<p< You are logged in. </p>
<p< More JSX markup can go here. </p>
</div>
): (
<div>
<p> You are not logged in. </p>
<p< Even more JSX markup can go here. </p>
</div>
)}
Please, share your insights, concerns and experiences about the topic and/or content of this article.