I am making a form using react.js. I am attempting to make a div appear and then disappear after the form has been submitted. How can I correctly do this?
My code currently just take me to a blank page when the form has been submitted.
Updated on request
constructor(props) {
super(props)
this.state = {
messageConfirm: ''
}
this.handleInput1 = this.handleInput1.bind(this);
this.handleInput2 = this.handleInput2.bind(this);
this.handleInput3 = this.handleInput3.bind(this);
this.sendEmail = this.sendEmail.bind(this)
this.submitForm = this.submitForm.bind(this)
}
resetForm() {
//reset form
}
validateForm() {
//validate form method
}
sendEmail(e) {
//send email method
}
confirmEmailMessage(){
this.setState({messageConfirm: "Thanks for your message"})
}
setTimeOut(){
this.setState({messageConfirm: ""}, 5000)
}
submitForm(e) {
e.preventDefault();
const isValid = this.validateForm();
if (isValid) {
// this.sendEmail(e)
this.confirmEmailMessage()
this.setTimeOut()
e.target.reset()
this.resetForm()
}
}
render() {
return (
<div className="container">
<div className="contact-form container-fluid d-flex justify-content-center bd-highlight" id="contact-form">
<form onSubmit={this.submitForm} data-testid="form">
//form code
</form>
</div>
<div>
</div>
</div>
)
}
}
React's this.setState's second argument is a callback function, not a static value. You probably meant to call setTimeout with a callback to update state. I also suggest changing the name of your function to something more meaningful, like resetMessage so it isn't as ambiguous. Then for completionist's sake you should save the timer ref and clear it when the component unmounts so you don't try to set state of an unmounted component.
constructor(props) {
super(props)
this.state = {
messageConfirm: '',
};
this.timerId = null;
}
resetMessage() {
this.timerId = setTimeout(() => {
this.setState({ messageConfirm: "" });
this.timerId = null;
}, 5000);
}
submitForm(e) {
e.preventDefault();
const isValid = this.validateForm();
if (isValid) {
this.confirmEmailMessage();
this.resetMessage();
e.target.reset();
this.resetForm();
}
}
...
componentWillUnmount() {
cleatTimeout(this.timerId);
}
Related
i have simple interactive app, and i want to render the CardList component, base on user search. the problem is i want to setTimeOut for the user search, and execute the function after 2000ms from when the user stoped typing.
here is the code, as you can see I managed to get it done, but its hacky and not really useful,im positive there is a better way to do this.
what I'm doing right now is to always change this.state.robots arrry, acording to the user input. notice the searchBox component has an input field
class App extends Component {
constructor(){
super();
this.state = {
robots: robots,
searchField: ''
}
}
onSearchange = (event) =>{
let timeOut = null;
this.setState({searchField: event.target.value,robots:robots});
event.target.onkeyup = (e) =>{
clearTimeout(timeOut);
timeOut = setTimeout(()=> {
const filterRobots = this.state.robots.filter(robot => {
return robot.name.toLowerCase().includes(this.state.searchField.toLowerCase());
})
this.setState({robots: filterRobots});
},2000);
}
}
render() {
return (
<div className = "tc">
<h1>RoboFriend</h1>
<SearchBox searchChange = {this.onSearchange}/>
<CardList robots = {this.state.robots} />
</div>
);
}
}
I would like to be able to send fillterRobots array dynamically to the CardList component so i can render the results properly
I would use something like lodash's debounce(). You don't just want a delay, you also want to cancel the previous delayed function execution if you receive a new event before the current delayed function has executed.
class TestComponent extends React.Component {
constructor(props) {
super(props);
this.state = { value: '' };
this.delayedCallback = _.debounce(this.handleChange, 1000);
}
handleChange(value) {
this.setState({ value });
}
onChange(event) {
event.persist();
this.delayedCallback(event.target.value);
}
render() {
return (
<div>
<input onChange={this.onChange.bind(this)} />
<p>{this.state.value}</p>
</div>
);
}
}
In my React-App, i use the Firebase SDK. If a user wants to reset his password, he will be redirected to a page within my app. If the code is valid, the component <PWResetConfirmForm /> should be rended. If the code is invalid, the component <PWResetOutdatedForm /> is to be rendered.
My Page Component looks like this:
class PWResetConfirmPage extends Component {
constructor(props) {
super(props);
this.state = {};
this.verfiyResetPassword = this.verfiyResetPassword.bind(this);
}
verfiyResetPassword() {
const params = (new URL(`http://dummy.com${this.props.location.search}`)).searchParams;
const code = params.get("oobCode")
auth.doVerfiyPasswordReset(code)
.then(function () {
return (
<div className="HomePage-Main">
<TopBar></TopBar>
<PWResetConfirmForm></PWResetConfirmForm>
</div>
);
})
.catch(function () {
return (
<div className="HomePage-Main">
<TopBar></TopBar>
<PWResetOutdatedForm></PWResetOutdatedForm>
</div>
);
})
}
render() {
return (
<div>
{this.verfiyResetPassword()}
</div>
);
}
}
export default PWResetConfirmPage
When i try to run, i get a blank page and not error.
Where is my issue and how can i fix that?
Thank you very much for your help and for your time
You will not be able to return JSX from within then()/catch() of auth.doVerfiyPasswordReset() like that. You can instead approach this by taking advantage of React.Component lifecycle method componentDidMount and using setState() to manipulate state properties for conditional rendering. I've added state properties to the component, one to track whether loading (API call has completed) and one to track whether the call was a success (then) or failure (catch). These properties are used to conditionally generate JSX content for rendering. This is assuming that verfiyResetPassword() is intended to run when the component is first mounted, instead of every time render() is called:
class App extends Component {
constructor() {
super();
this.state = {
isResetVerified: null,
loading: true
};
}
componentDidMount() {
this.verfiyResetPassword();
}
verfiyResetPassword() {
const params = (new URL(`http://dummy.com${this.props.location.search}`)).searchParams;
const code = params.get("oobCode")
auth.doVerfiyPasswordReset('foobar')
.then(() => {
this.setState({
...this.state,
isResetVerified: true,
loading: false
});
})
.catch(() => {
this.setState({
...this.state,
isResetVerified: false,
loading: false
});
})
}
getContent() {
if (this.state.loading) {
return (
<div>Loading...</div>
);
} else {
if (this.state.isResetVerified) {
return (
<div className="HomePage-Main">
<TopBar></TopBar>
<PWResetConfirmForm></PWResetConfirmForm>
</div>
);
} else {
return (
<div className="HomePage-Main">
<TopBar></TopBar>
<PWResetOutdatedForm></PWResetOutdatedForm>
</div>
);
}
}
}
Here is a basic example in action.
Also, in the constructor this.verfiyResetPassword = this.verfiyResetPassword.bind(this); would only be needed if verfiyResetPassword() is executed by a DOM event such as button onClick or similar.
Hopefully that helps!
I could still fix the error myself:
class PWResetConfirmPage extends Component {
constructor(props) {
super(props);
this.state = {
isValid: false,
code: "",
};
this.verfiyResetPassword = this.verfiyResetPassword.bind(this);
}
componentDidMount() {
const params = (new URL(`http://dummy.com${this.props.location.search}`)).searchParams;
const code = params.get("oobCode")
this.setState({code:code})
auth.doVerfiyPasswordReset(code)
.then(() => {
this.setState({
...this.state,
isValid: true,
});
})
.catch(() => {
this.setState({
...this.state,
isValid: false,
});
})
}
verfiyResetPassword() {
if (this.state.isValid) {
return (
<div>
<TopBar></TopBar>
<PWResetConfirmForm code={this.state.code}></PWResetConfirmForm>
</div>
);
} else {
return (
<div>
<TopBar></TopBar>
<PWResetOutdatedForm></PWResetOutdatedForm>
</div>
);
}
}
render() {
return (
<div className="HomePage-Main">
{this.verfiyResetPassword()}
</div>
);
}
}
export default PWResetConfirmPage
I have a question for you... On my react app, I have a <input></input> but I would like the user to be able to keep his message thanks to the local storage.
class Profil extends Component {
message() {
if (localStorage != 'undefined'){
document.getElementById('message').value = localStorage.getItem('message');
}
else {
alert("localStorage is not supported");
}
}
render() {
return (
<div>
<input name="message" onChange={() => this.message()}></input>
</div>
);
}}
With that, when I put a letter, I have directly an error message :
TypeError: localStorage.getItem(...) is null
and this line is false :
document.getElementById('message').value = localStorage.getItem('message');
What do I have to do ?
Instead of manipulating the DOM manually, you could keep the message in state and update the localStorage value on every change, and also read the value from localStorage when the component is created.
Example
class Profile extends React.Component {
constructor() {
super();
this.state = {
message: localStorage.getItem("message") || ""
};
}
onChange = event => {
const message = event.target.value;
localStorage.setItem("message", message);
this.setState({ message });
};
render() {
return <input value={this.state.message} onChange={this.onChange} />;
}
}
In functional component in the following way we can able to store and restore the values from localstorage using useEffect hook
const mounted = useRef()
const [ formValues, setFormValues ] = useState({firstname: '', lastname: ''})
function supportsLocalStorage() {
try {
return 'sessionStorage' in window && window['sessionStorage'] !== null;
} catch(e){
return false;
}
}
useEffect(() => {
if (!mounted.current && supportsLocalStorage) {
const progress = JSON.parse(localStorage.getItem('formValues'))
if (progress !== null) {
for (const x in formValues) {
formValues[x] = progress[x]
}
}
mounted.current = true
} else {
const filteredState = formValues
localStorage.setItem('formValues', JSON.stringify(filteredState))
}
}, [ formValues ])
I have a React component that pushes song IDs to the url when the state changes. My problem is that when a user clicks 'back' on their browser, I need to change the state of my SongApp component. How do I do this?
class SongApp extends React.Component {
constructor(props) {
super(props);
this.state = {
song: props.songId
}
this.setSong = this.setSong.bind(this);
}
setSong(e) {
var songId = e.target.id;
this.setState({song: songId})
window.history.pushState({song: songId}, '', '?s='+songId)
}
render() {
var id = this.state.song;
var content = id ? <SongDisplay lyrics={ this.props.songData[id].lyrics } /> : <SongIndex songData={this.props.songData} setSong={this.setSong}/>
return(
<div className="song-app">
{content}
</div>
)
}
}
window.addEventListener('popstate', function(event) {
console.log('popstate fired!');
debugger;
if(event.state.song) {
// change SongApp state
}
});
I found out you can attach the component's method to a listener:
componentDidMount() {
window.addEventListener("popstate", this.setSongFromHistory);
}
setSongFromHistory(e) {
if(e.state.song){
e.preventDefault(); // stop request to server for new html
e.stopPropagation();
this.setState({song: e.state.song});
$('html,body').scrollTop(0);
}
}
I'm trying to update the state of my component inside of an eventListener. I'm getting the following console error:
'Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the Header component'
This is my component code:
class Header extends React.Component {
constructor(props) {
super(props);
this.state = {
fixed: false
}
}
handleScroll(event) {
this.setState({
fixed: true
});
}
componentDidMount() {
window.addEventListener("scroll",() => {
this.handleScroll();
});
}
componentWillUnmount() {
window.removeEventListener("scroll",() => {
this.handleScroll();
});
}
render() {
var {
dispatch,
className = "",
headerTitle = "Default Header Title",
onReturn,
onContinue
} = this.props;
var renderLeftItem = () => {
if (typeof onReturn === 'function') {
return (
<MenuBarItem icon="navigation-back" onClick={onReturn}/>
)
}
};
var renderRightItem = () => {
if (typeof onContinue === 'function') {
return (
<MenuBarItem icon="navigation-check" onClick= {onContinue}/>
)
}
};
return (
<div className={"header " + className + this.state.fixed}>
{renderLeftItem()}
<div className="header-title">{headerTitle}</div>
{renderRightItem()}
</div>
)
}
}
Header.propTypes = {
};
let mapStateToProps = (state, ownProps) => {
return {};
};
export default connect(mapStateToProps)(Header);
IMHO this is because you do ont unregister the function as you expect it, and a scroll event is sent after an instance of this component has been unmounted
try this:
componentDidMount() {
this._handleScroll = this.handleScroll.bind(this)
window.addEventListener("scroll", this._handleScroll);
}
componentWillUnmount() {
window.removeEventListener("scroll", this._handleScroll);
}