Cannot flush updates when React is already rendering - Sweet - reactjs

I get the following error when rendering a SweetAlert.. not sure how to solve?
index.js:1451 Warning: unstable_flushDiscreteUpdates: Cannot flush updates when React is already rendering.
in n (at LoginOverlay/index.js:248)
in div (at LoginOverlay/index.js:82)
in Formik (at LoginOverlay/index.js:66)
in LoginOverlay (created by ConnectFunction)
in ConnectFunction (created by Context.Consumer)
in Route (at Routes.js:270)
in div (created by ForwardRef(MotionComponent))
in ForwardRef(MotionComponent) (at Routes.js:263)
in Switch (at Routes.js:262)
in PresentationLayout (at Routes.js:261)
in Route (at Routes.js:260)
in Switch (at Routes.js:258)
in Suspense (at Routes.js:257)
in PresenceChild (created by AnimatePresence)
Code:
const LoginOverlay = (props) => {
const history = useHistory();
const { magic, login, clearError } = props;
// useEffect(() => {
// if (magic.entity?.loggedIn && localStorage.getItem('did_token')) {
// //history.push('/business/home');
// }
// }, [magic.loading, magic.showInfoDialog]);
return (
<Formik
initialValues={{
email: ''
}}
validationSchema={yup.object().shape({
email: yup
.string()
.email('Must be valid email address')
.min(3)
.required('Email is required')
})}
onSubmit={async (values, actions) => {
console.log(values, props);
props.login(values.email);
}}>
{(props) => (
<div className="app-wrapper min-vh-100 bg-white">
<div className="hero-wrapper w-100 bg-composed-wrapper bg-midnight-bloom min-vh-100">
<div className="flex-grow-1 w-100 d-flex align-items-center">
<div
className="bg-composed-wrapper--image opacity-2"
style={{ backgroundImage: 'url(' + hero6 + ')' }}
/>
<div className="bg-composed-wrapper--bg bg-second opacity-7" />
<div className="bg-composed-wrapper--content p-3 p-md-5">
<Container>
<Card className="rounded-sm modal-content p-3 bg-white-10">
<Card className="rounded-sm overflow-hidden shadow-xxl font-size-sm p-3 p-sm-0">
<Row className="no-gutters">
<Col
lg="6"
className="d-flex align-items-center justify-content-center flex-column">
<Form onSubmit={props.handleSubmit}>
<div className="divider-v divider-v-lg d-none d-lg-block" />
<div className="text-center mt-4">
<h1 className="font-size-xxl mb-1 font-weight-bold">
Login
</h1>
<br />
<p className="mb-0 text-black-50">
Enter you email below to sign up or sign in to
your account
</p>
<br />
<div className="mb-0 text-black-50">
<div className="form-group mb-6">
<Input
type="text"
{...props.getFieldProps('email')}
placeholder="Email"
invalid={
props.errors.email && props.touched.email
}
valid={
!props.errors.email && props.touched.email
}
/>
<FormFeedback>
{props.errors.email}
</FormFeedback>
</div>
<div className="text-center py-4">
<Button
type="submit"
disabled={
magic.loading || props.isSubmitting
}
className="font-weight-bold w-50 my-2"
color="second">
Submit
</Button>
{/* <LaddaButton
type="submit"
className="m-2 btn btn-second"
loading={(magic.loading || props.isSubmitting)}
onClick={(e) => {
e.preventDefault();
props.submitForm();
}}
data-style={EXPAND_LEFT}>
Submit
</LaddaButton> */}
</div>
</div>
</div>
</Form>
</Col>
<Col
lg="6"
className="d-flex align-items-center justify-content-center flex-column bg-secondary">
<div className="p-3">
<div className="p-4">
<div className="d-block d-xl-flex">
<div className="mt-0 mt-xl-1 mb-md-2 mb-lg-0">
<FontAwesomeIcon
icon={['far', 'heart']}
className="font-size-xl text-first"
/>
</div>
<div className="pl-0 pl-xl-3">
<div className="text-black font-weight-bold font-size-lg mb-1">
Have we anything for here?
</div>
<p className="mb-0 text-black-50">
Have we anything for here?
</p>
</div>
</div>
</div>
<div className="p-4">
<div className="d-block d-xl-flex">
<div className="mt-0 mt-xl-1 mb-md-2 mb-lg-0">
<FontAwesomeIcon
icon={['far', 'lightbulb']}
className="font-size-xl text-first"
/>
</div>
<div className="pl-0 pl-xl-3">
<div className="text-black font-weight-bold font-size-lg mb-1">
Have we anything for here?
</div>
<p className="mb-0 text-black-50">
Have we anything for here?
</p>
</div>
</div>
</div>
<div className="p-4">
<div className="d-block d-xl-flex">
<div className="mt-0 mt-xl-1 mb-md-2 mb-lg-0">
<FontAwesomeIcon
icon={['far', 'user']}
className="font-size-xl text-first"
/>
</div>
<div className="pl-0 pl-xl-3">
<div className="text-black font-weight-bold font-size-lg mb-1">
Have we anything for here?
</div>
<p className="mb-0 text-black-50">
Have we anything for here?
</p>
</div>
</div>
</div>
</div>
</Col>
</Row>
</Card>
</Card>
</Container>
</div>
</div>
<div className="hero-footer w-100 pb-4">
<Container>
<div className="py-3 d-block d-lg-flex font-size-xs justify-content-between">
<div className="text-center d-block mb-3 mb-md-0 text-white">
XXXXX 2021
</div>
<Nav className="nav-transparent justify-content-center">
<NavItem>
<NavLinkStrap
className="text-white-50"
href="#/"
onClick={(e) => e.preventDefault()}>
Privacy Policy
</NavLinkStrap>
</NavItem>
<NavItem>
<NavLinkStrap
className="text-white-50"
href="#/"
onClick={(e) => e.preventDefault()}>
Terms of Service
</NavLinkStrap>
</NavItem>
</Nav>
</div>
</Container>
</div>
</div>
<SweetAlert
title="Check your email"
show={!magic.entity?.loggedIn && magic.showInfoDialog}
html={`We emailed a magic link to ${props.values.email}</br>Click the link in your email and then return to this tab to log in or sign up.</br>`}
type="info"
showCancelButton={false}
showConfirmButton={false}
/>
<SweetAlert
title="Error"
show={!magic.entity?.loggedIn && magic.errorMessage}
html={magic.errorMessage}
type="error"
showCancelButton={false}
showConfirmButton={true}
onConfirm={() => clearError()}
/>
{
(localStorage.getItem("did_token") || magic.entity?.loggedIn ) && <Redirect to="/business/home" />
}
</div>
)}
</Formik>

Please check for any other alerts that is dependent. It happens when you are trying to access multiple alerts

Related

Can't put bg image in react.js

enter image description here
import React from 'react'
import pic from './pic2.png'
const Header = () => {
return (
<div className="container-fluid">
<div className="row text-center align-items-center ">
<div className="bg col-md-6 ">
<div style={{ backgroundImage: `url(/pic.jpg)` }}>
</div>
</div>
<div className=" col-6">
<h1>Login</h1>
<form className='loginf'>
<label className='pt-4'> Username </label> <br></br>
<input type='text'className='form-rounded p-1' placeholder=' username'/>
<br></br> <br></br>
<label> Password </label> <br></br>
<input type='password' className='form-rounded p-1' placeholder=' password'/>
</form>
<br></br>
<button className='btn btn-basic form-rounded2'>Login </button>
</div>
</div>
</div>
)
}
export default Header
You have imported a picture, so u can try use object {pic}, not a link to picture
Instead <div style={{ backgroundImage: url(/pic.jpg) }}>
Try: <div style={{ backgroundImage: ``url(${pic})` }}>
Similar issue: Setting a backgroundImage With React Inline Styles

Search not found message is displaying while loading the spinner

If my search is not returning any results, then I will display a message Does not match any results! in Home.js screen. I would like to make the display of message more efficiently. I have a loading spinner running while loading the home screen, you could see that text is also displaying along while running the spinner ( attached screen shot)
I would like to display that message only if the search is not returning any results/ searched data. In all other cases, it should be hidden ...How can I put that condition ?
const [playerList, setPlayerList] = useState([]);
const [searchTerm, setSearchTerm] = useState("");
const handleChange = event => {
setSearchTerm(event.target.value);
};
useEffect(() => {
const results = playerList.filter(player =>
player.name.toLowerCase().includes(searchTerm) || player.name.toUpperCase().includes(searchTerm) || player.position.toLowerCase().includes(searchTerm)
|| player.position.toUpperCase().includes(searchTerm)
);
setSearchResults(results);
}, [searchTerm, playerList]);
return (
<div className="App">
<div className="wrapper">
<div className="playerList_header">
<h1>Players</h1>
<label>
<div className="playerSearch_Home">
<div className="playerSearch_Icon">
<img alt="" src="/images/search-image-player.jpg"></img>
</div>
<input type="text" className="playerSearch_Home_Input" placeholder="Search players..." value={searchTerm} onChange={handleChange} />
</div>
</label>
</div>
<div>
{!searchResults.length && (<div> <p className="noSearchData"> Does not match any results! </p> </div>)}
<div className="playerList_home_page">
{isLoading ? (
<div className="loader">
<div className="bubble"></div>
<div className="bubble"></div>
<div className="bubble"></div>
<div className="bubble"></div>
</div>
) : (
<div className="grid-container">
{
searchResults.map(({ id, image, position, phonenumber, name }) => (
<div key={id} className="grid-item">
{
deleteIcon.show && (
<span className="deletePlayerButton" onClick={deletePlayer(id)}>
<img className="deletePlayerimg" src="/images/delete.png"></img>
</span>
)}
<div>
<img alt="" className="playerProfilePic_home_tile" key={image} src={image}></img>
</div>
<div className="playerProfile_grid_border">
<span className="rec_name_position_data">
<h3 key={name}>{name}</h3>
<span className="playerPosition_home_tile" key={position}>{position}</span>
</span>
</div>
<span className="phoneNumber_home">
<img src="/images/phone.png" alt={"phoneTooltip.show"} key={id} name="phoneNumberhomeicon" onClick={displayPhoneToolTip(id)} />
</span>
{phoneTooltip === id && (
<div className="tooltip_PhoneNumber_home" key={phonenumber}>{phonenumber}</div>
)}
</div>
))
}
</div>
)}
</div>
</div>
</div>
<AlertDialog
onDelete={onDelete}
open={deleteDialog}
onClose={() => setDeleteDialog(false)}
playerId={playerId}
/>
</div>
);
Add another condition with isLoading like so
{!searchResults.length && !isLoading && (<div> <p className="noSearchData"> Does not match any results! </p> </div>)}

Return a message for a search display zero search results in react hooks

If a search in React hooks has returned zero results, I would like to display a text message "No data available for the search criteria !" in the page ? I have tried a condition, but not sure where to add.
if(results.length == 0) {
return (
<div>No data available for the search criteria !</div>
)
}
const [searchTerm, setSearchTerm] = useState("");
const [searchResults, setSearchResults] = useState([]);
const handleChange = event => {
setSearchTerm(event.target.value);
};
useEffect(() => {
const results = playerList.filter(player =>
player.name.toLowerCase().includes(searchTerm) || player.name.toUpperCase().includes(searchTerm) || player.position.toLowerCase().includes(searchTerm)
|| player.position.toUpperCase().includes(searchTerm)
);
setSearchResults(results);
}, [searchTerm]);
//
return (
<div className="App">
<label>
<div className="playerSearch_Home">
<div className="playerSearch_Icon">
<img src="/images/search-image-player.jpg"></img>
</div>
<input type="text" className="playerSearch_Home_Input" placeholder="Search players..." value={searchTerm} onChange={handleChange} />
</div>
</label>
<div className="playerList_home_page">
<div className="grid-container">
{
searchResults.map(player => {
return (
<div className="grid-item">
<div>
<img className="playerProfilePic_home_tile" key={player.image} src={player.image}></img>
</div>
<div className="playerProfile_grid_border">
<h3 key={player.name}>{player.name}</h3>
<span className="playerPosition_home_tile" key={player.position}>{player.position}</span>
</div>
</div>
);
})
}
</div>
</div>
</div>
);
You use simply ternary operator, something like this :
{
searchResults.length ?
searchResults.map(player => {
return (
<div className="grid-item">
<div>
<img className="playerProfilePic_home_tile" key={player.image} src={player.image}></img>
</div>
<div className="playerProfile_grid_border">
<h3 key={player.name}>{player.name}</h3>
<span className="playerPosition_home_tile" key={player.position}>{player.position}</span>
</div>
</div>
);
}) :
"No data available for the search criteria !"
}
You can use ternary operator based on the length of searchResults.
Please check the code below:
return (
<div className="App">
<label>
<div className="playerSearch_Home">
<div className="playerSearch_Icon">
<img src="/images/search-image-player.jpg"></img>
</div>
<input type="text" className="playerSearch_Home_Input" placeholder="Search players..." value={searchTerm} onChange={handleChange} />
</div>
</label>
<div className="playerList_home_page">
<div className="grid-container">
{
**searchResults.length > 0 ?**
searchResults.map(player => {
return (
<div className="grid-item">
<div>
<img className="playerProfilePic_home_tile" key={player.image} src={player.image}></img>
</div>
<div className="playerProfile_grid_border">
<h3 key={player.name}>{player.name}</h3>
<span className="playerPosition_home_tile" key={player.position}>{player.position}</span>
</div>
</div>
);
})
**} : <p>Zero records found for the Search</p>**
</div>
</div>
</div>
);
You need to make a conditional render based on the length of the search results array. If there's nothing in the array, then it will show a message.
{!searchResults.length && (<div> <p> No data available for the search criteria ! </p> </div>) }
return (
<div className="App">
<label>
<div className="playerSearch_Home">
<div className="playerSearch_Icon">
<img src="/images/search-image-player.jpg"></img>
</div>
<input type="text" className="playerSearch_Home_Input" placeholder="Search players..." value={searchTerm} onChange={handleChange} />
</div>
</label>
{!searchResults.length && (<div> <p> No data available for the search criteria ! </p> </div>) }
<div className="playerList_home_page">
<div className="grid-container">
{
searchResults.map(player => {
return (
<div className="grid-item">
<div>
<img className="playerProfilePic_home_tile" key={player.image} src={player.image}></img>
</div>
<div className="playerProfile_grid_border">
<h3 key={player.name}>{player.name}</h3>
<span className="playerPosition_home_tile" key={player.position}>{player.position}</span>
</div>
</div>
);
})
}
</div>
</div>
</div>
);

JSX Bootstrap unterminated

I'm using bootstrap with react, and the compiler is failing on grounds that there is unterminated JSX contents. I've been through it a dozen times, and whichever way the ternary results, I don't see any unclosed tags. I'm new to react, so I suppose something else must be wrong. To cut down on code, I've only included the return():
<div className="container-fluid">
<div className="row">
<div className="col-12">
<SearchBar />
</div>
</div>
{this.state.results ? (
<div className="row">
<div className="col-12">
<div className="row">
<div className="col-6 offset-md-3">
<div className="row">
<div className="col">
<InputRange
maxValue={this.state.maxPrice}
minValue={this.state.minPrice}
value={this.state.priceRange}
formatLabel={x => {
return `£${x}`;
}}
onChange={value => this.setState({ priceRange: value })}
/>
</div>
</div>
<div className="row">
<div className="col">
Selection: {visibleResults.length}
</div>
</div>
<div>
</div>
<div className="row">
<div className="col-6 offset-md-3">
{visibleResults.map((item, index) => (
<Link to={`/listing/${item.id}`}>
<SearchResult result={item} key={index} />
</Link>
))}
</div>
</div>
</div>
</div>
) : (
<div className="row">
<div className="col">
<h1>LOADING...</h1>
</div>
</div>
)}
</div>
)
You need to close the div here:
<div className="row">
<div className="col">
Selection: {visibleResults.length}
</div>
</div>
<div> // This should be </div>
</div>

Show Mobile component ReactJS

import React, { Component } from 'react';
class BigText extends Component {
render() {
return (
<div>
<div>
<h1 className="row px-2">Big Text Notification</h1>
<hr />
<div className="row px-1 py-2 animated fadeIn">
<div className="px-1" style={{ width:50 + '%' }}><br />
<div className="mb-1">
<input type="text"
className="form-control"
placeholder="Title"
name="title"
/>
</div>
<div className="mb-1">
<textarea
className="form-control"
placeholder="Text"
name="text"
/>
</div>
<div className="mb-1">
<textarea
className="form-control"
placeholder="Summary"
name="summary"
/>
</div>
<br />
<div className="row px-2" >
<div>
<button className="nav-link btn btn-block btn-info" onClick={this.handleClick} >Save</button>
</div>
<div className="px-1">
<button className="nav-link btn btn-block btn-danger"> Cancel</button>
</div>
</div><br />
</div>
</div>
</div>
</div>
)
}
}
export default BigText;
i had added the code snippet
What I'm doing is I'm trying to add a Mobile frame as component on side of the fields so that whatever i write in the fields will be shown in that mobile being shown as the component on the screen
so i wanted to know how to add that mobile and how to show text in it as well

Resources