This question already has answers here:
The useState set method is not reflecting a change immediately
(15 answers)
Closed 1 year ago.
state isn't reflecting change immediately causing me to have to fun onSubmit twice to get the form to submit
If you want to perform an action on state update, you need to use the useEffect hook, much like using componentDidUpdate in class components since the setter returned by useState doesn 't have a callback pattern
from Link to suggested question
But I'm Honestly confused on how to implement an on Submit into a use effect I'm sorry I'm new to react
const onSubmit = async(data) = > {
setNameError(nameValidation(data.name));
setphoneError(phoneNumberValidation(data.phoneNumber));
setEmailError(emailValidation(data.email));
setMessageError(messageValidation(data.message));
//Here is where I'm getting caught up, in react dev tools the above states are being set to false but below the noErrors Variable is still reading false after all conditions check true the no Errors is still getting the old values for some reason I even used a settimeout method.
let noErrors = (!nameError && !phoneError && !emailError && !messageError);
if (noErrors) {
try {
// const templateParams = {
// name: data.name,
// email: data.email,
// number: data.phoneNumber,
// message: data.message,
// };
// await emailjs.send(
// process.env.REACT_APP_SERVICE_ID,
// process.env.REACT_APP_TEMPLATE_ID,
// templateParams,
// process.env.REACT_APP_USER_ID
// );
reset();
toastifySuccess();
} catch (e) {
console.log(e);
}
}
};
const hasCharacter = /[a-zA-Z]/g;
export const nameValidation = function nameValidation(name) {
if (name.length > 30) {
return 'This field only accepts 30 characters';
}
if (name.length < 5) {
return 'This field requires five characters';
}
if (/\d/.test(name)) {
return ' This field cannot contain numbers';
}
if (!name.includes(' ')) {
return 'This Field Requires A Space';
}
return false;
};
export const phoneNumberValidation = (number) = > {
if (number.length !== 10) {
return 'A Phone Number Must be ten digits';
}
if (hasCharacter.test(number)) {
return 'A Phone Number Shouldnt Contain A Letter';
}
return false;
};
export const emailValidation = (email) = > {
if (email.length > 30) {
return 'This field only accepts 30 characters';
}
if (email.length < 5) {
return 'This field requires five characters';
}
if (!email.includes('#')) {
return 'Email Addresses require # Symbol';
}
return false;
};
export const messageValidation = (message) = > {
if (message.length > 500) {
return 'This field only accepts 500 characters';
}
if (message.length < 5) {
return 'This field requires five characters';
}
return false;
};
Here there are 2 ways to solve your issue.
Store error in local variable and use those variables to setState and check noError.
const onSubmit = async(data) = > {
const nameErr = nameValidation(data.name);
const phoneErr = nameValidation(data.phoneNumber);
const emailErr = nameValidation(data.email);
const messageErr = nameValidation(data.message);
setNameError(nameErr);
setphoneError(phoneErr);
setEmailError(emailErr);
setMessageError(messageErr);
let noErrors = (!nameErr && !phoneErr && !emailErr && !messageErr);
// rest of your code
}
use useEffect to calculate noErrors
const onSubmit = async(data) = > {
setNameError(nameValidation(data.name));
setphoneError(phoneNumberValidation(data.phoneNumber));
setEmailError(emailValidation(data.email));
setMessageError(messageValidation(data.message));
}
useEffect(() => {
const submitForm = async () => {
let noErrors = (!nameErr && !phoneErr && !emailErr && !messageErr);
if (noErrors) {
try {
// const templateParams = {
// name: data.name,
// email: data.email,
// number: data.phoneNumber,
// message: data.message,
// };
// await emailjs.send(
// process.env.REACT_APP_SERVICE_ID,
// process.env.REACT_APP_TEMPLATE_ID,
// templateParams,
// process.env.REACT_APP_USER_ID
// );
reset();
toastifySuccess();
} catch (e) {
console.log(e);
}
}
}
submitForm();
},[nameError, phoneError, emailError, messageError])
Related
Below function always submit the form at the first render of the component even if firstName and lastName fields are empty.
const [formErrors, setFormErrors] = useState({});
const registerUser = () => {
if (firstName === "") {
setFormErrors((prev) => {
return { ...prev, firstName: "Firstname is required!" };
});
}
if (lastName === "") {
setFormErrors((prev) => {
return { ...prev, lastName: "Lastname is required!" };
});
}
if (Object.keys(formErrors).length === 0) {
console.log("Form Submitted");
} else {
console.log("Failed");
}
}
At first render, the value of Object.keys(formErrors).length is 0, but the errors are shown on the screen, weird!.
I tried to use useRef which worked, but it doesn't display the errors on screen because the component doesn't rerender with useRef.
How do I prevent this form from being submitted if errors still exist?
Thank You
Your problem is that you have called set state (formErrors) and then you try to read it immediately. So when you set it, it hasn't been updated yet, you are reading stale data.
To fix it use some local variable in that function (initially you may want to initialize it with state data) to keep track of errors, and then at the end when you are done with it, put that in state.
Something like this
const registerUser = () => {
let errors = {
...formErrors
}
if (firstName === "") {
errors.firstName = "Firstname is required!"
}
if (lastName === "") {
errors.lastName = "lastName is required!"
}
if (Object.keys(errors).length === 0) {
console.log("Form Submitted");
} else {
console.log("Failed");
}
setFormErrors(errors)
}
How do I take input from user multiple times, store it, and then send an embed with the inputs?
User types command ?start
Bot replies "Hi type your name here"
User types a name, then it is stored in a variable
Bot asks again "Type your favourite game now"
User types games, it is again stored in a variable
Then the variables are taken and then made into an embed
const embed = new Discord.MessageEmbed()
.setTitle("Hobbies")
.setThumbnail(messsage.author.user.displayAvatarURL())
.addDescription("<name>")
.addDescription("<game>")
.setTimestamp();
message.channel.send(embed);
to solve that i created little "Scripts", just predefined Routines for each state of the command
script.js
class Script {
constructor (user, options, callback) {
if (!user.send) {
throw "Invalid Userhandle";
}
this.user = user;
this.options = options;
this.state = 0;
this.callback = callback;
this.responses = [];
if (!!this.options.greeting) {
this.user.send(this.options.greeting)
.then()
.catch(() => console.log(JSON.stringify(this.options.greeting)));
}
};
interpretMessage(message) {
if (!this.options.validator[this.state] || typeof this.options.validator[this.state] !== 'function') {
if (!!this.callback) {
this.callback(this.user, this.responses, false);
return;
} else {
throw "Invalid User Gatherer Object";
}
}
const [msg, steps] = this.options.validator[this.state](message, this.responses);
this.user.send(msg)
.then()
.catch(() => console.error(msg));
if (steps > 0 || steps < 0) {
if (!!this.responses && !!this.responses[this.state]) {
this.responses[this.state] = message;
} else {
this.responses.push(message);
}
this.state += steps;
}
if (this.state >= this.options.validator.length) {
this.callback(this.user, this.responses, false);
}
};
};
module.exports = Script;
I use this Method only in private Messages, that's the reason for my naming:
msg_script.js
const Script = require('./classes/script');
let privateInfoGatherer = {};
let privateInfoGathererCallback = {};
function deletePrivateInfoGatherer(usr, out) {
if (!usr || !usr.id) {
return;
}
privateInfoGathererCallback[usr.id](usr, out);
delete privateInfoGatherer[usr.id];
delete privateInfoGathererCallback[usr.id];
};
function PrivateInfoGatherer (usr, opts, callback) {
if (!usr || !usr.id || !opts || !callback) {
return;
}
privateInfoGatherer[usr.id] = new Script(usr, opts, deletePrivateInfoGatherer);
privateInfoGathererCallback[usr.id] = callback;
};
function checkPrivateMessage(msg, args) {
if (!msg || !msg.author || !privateInfoGatherer[msg.author.id] || msg.guild) {
return;
}
privateInfoGatherer[msg.author.id].interpretMessage(msg.content);
};
module.exports = {
provide: {
PrivateInfoGatherer: PrivateInfoGatherer,
},
events: {
message: checkPrivateMessage,
}
};
my final usage looked something like this:
const ressource = require('./classes/ressource');
function interpretAuth(msg, args, provider) {
const usr = msg.author;
const stage_1 = (msg) => {
let steps = msg.match("^([A-Za-z0-9_ ]{4,32})$") ? 1 : 0;
let ret;
if (msg === 'abort') {
steps = 100; // will result in ending the script
} else {
ret = steps === 1 ? 'And now your Password' : 'Gimme your username';
}
return [ret, steps];
};
const stage_2 = (msg) => {
let steps = msg.match("^([A-Za-z0-9\\!\\#\\#\\%\\&\\_\\(\\)\\*\\-\\$\\^\\[\\]]+)$") ? 1 : 0;
let ret;
if (msg === 'abort') {
steps = 100;
} else {
ret = steps === 1 ? 'I will check the Auth' : 'Your Password man...';
}
return [ret, steps];
};
const options = {
greeting: 'Ok for Authrole i need your login, so first your username pls',
validator: [
stage_1,
stage_2,
]
};
const callback = (usr, out) => {
const [username, password] = out;
// Now we have all, do what ever you want with it.
};
provider.PrivateInfoGatherer(usr, options, callback);
};
Despite looking and following numerous answers here at stackoverflow,I have still failed to refactor this code to abide by the ESLint no-loop-func.
I keep getting the following warning, despite my efforts to refactor the code:
Compiled with warnings.
Function declared in a loop contains unsafe references to variable(s) 'lastResult', 'biologyBooks', 'page' no-loop-func
Here's the code:
import React from 'react';
import { apiFullCall } from '../../apiHelper';
const MyComponent = props => {
const [state, setState] = React.useState({ total: 0, biologyBooksByAuthor: [] });
let isLoaded = React.useRef(true);
const token = sessionStorage.getItem('token');
const authorID = sessionStorage.getItem('author_id');
const getBooks = async() => { // fetch items
let page = 1;
let scienceBooks, biologyBooks;
// create empty arrays to store book objects for each loop
let scienceBooks = biologyBooks = [];
// create a lastResult object to help check if there is a next page
let lastResult = { next: null };
do { // the looping - this is what I have failed to refactor
try {
await apiFullCall( // Make API calls over paginated records
'',
token,
'get',
`books/?author_id=1&page=${page}`
).then(res => {
if (res) {
const { status, body } = res;
if (status === 200 || status === 201) {
lastResult = body; // assign lastResult to pick "next"
body &&
body.results &&
body.results.map(eachBook => { // we map() over the returned "results" array
// the author with queried "author_id" writes science books;
// so we add each book (an object) into the science category
scienceBooks.push(eachBook);
// We then filter the author's biology books (from other science books)
biologyBooks = scienceBooks.filter(
({ is_biology }) =>
typeof(is_biology) === "boolean" && is_biology === true
);
return null;
}
);
// increment the page with 1 on each loop
page++;
}
}
}).catch(error => console.error('Error while fetching data:', error));
} catch (err) { console.error(`Oops, something went wrong ${err}`); }
// keep running until there's no next page
} while (lastResult.next !== null);
// update the state
setState(prevState => ({
...prevState, total: scienceBooks.length, biologyBooksByAuthor: biologyBooks,
}));
};
React.useEffect(() => { // fetch science books by author (logged in)
if (isLoaded && authorID) {
getBooks();
};
return function cleanup() {...}; // clean up API call, on unmount
}, [isLoaded, authorID]);
return (
// render the JSX code
);
}
Please note that I actually declared the said variables lastResult, biologyBooks and page outside the "do-while".
Any help or clues will be greatly appreciated.
The function the warning is referring to is the .then callback, if you're using async/await stick to it, try removing the .then part by assigning the result to a variable instead and remove the unnecessary .map, you can concatenate previous results with spread operator or .concat.
import React from 'react';
import { apiFullCall } from '../../apiHelper';
const MyComponent = props => {
const [state, setState] = React.useState({
total: 0,
scienceBooksByAuthor: [],
});
const isLoaded = React.useRef(true);
const token = sessionStorage.getItem('token');
const authorID = sessionStorage.getItem('author_id');
const getBooks = async () => {
// fetch items
let page = 1;
let scienceBooks = [];
// create a lastResult object to help check if there is a next page
let lastResult = { next: null };
do {
// the looping - this is what I have failed to refactor
try {
const res = await apiFullCall(
// Make API calls over paginated records
'',
token,
'get',
`books/?author_id=1&page=${page}`,
);
if (res) {
const { status, body } = res;
if (status === 200 || status === 201) {
lastResult = body; // assign lastResult to pick "next"
// concatenate new results
scienceBooks = [
...scienceBooks,
...((lastResult && lastResult.results) || []),
];
// increment the page with 1 on each loop
page += 1;
}
}
} catch (err) {
console.error(`Oops, something went wrong ${err}`);
}
// keep running until there's no next page
} while (lastResult.next !== null);
const biologyBooks = scienceBooks.filter(
({ is_biology }) =>
typeof is_biology === 'boolean' && is_biology === true,
);
// update the state
setState(prevState => ({
...prevState,
total: scienceBooks.length,
scienceBooksByAuthor: scienceBooks,
}));
};
React.useEffect(() => {
// fetch science books by author (logged in)
if (isLoaded && authorID) {
getBooks();
}
return function cleanup() {...}; // clean up API call, on unmount
}, [isLoaded, authorID]);
return (
// render the JSX code
);
};
A button calls the function signAllBrowsed, which contains two other functions:
loadSafetyLetters is a hook that makes a database call for some data and sets it in context
signAll is a hook that tries to access data in context to do something with it
Context is getting set properly, but when signAll accesses it, the data is not updated. Is there a way to access the updated context without directly passing it to the 2nd function? Or is there a way to call a callback once context is updated and accessible? Seems the updated context is only available after a re-render.
The component containing signAllBrowsed and the 2 hooks are siblings.
code in above image:
setModalVisible(true)
const logHeader = 'SafetyLetterHome::SignAllBrowsed'
try {
const response = await loadSafetyLetters(false) // before beginning sign all, get a fresh list of letters from db
if (Configs.SHOW_REQUEST_LOGS) console.log(`${logHeader} response`, response)
if (response === 'no api error') {
await signAll()
navigation.navigate('SafetyLetterSign')
}
} catch (error) {
const errorMessage = error.status && error.status.message ? error.status.message : error
Alert.alert('Database Error', errorMessage)
console.log(`${logHeader}`, errorMessage)
}
}
loadSafetyLetters calls the loadLetters hook:
const [getLetters] = useGetLetters()
const [sortLetters] = useSortLetters()
const [hasAPIError] = useHasAPIError()
const navigation = useNavigation()
const { setModalVisible, setShowSignAll, setSortedLetters, setUnsortedLetters } = useContext(SafetyContext)
const loadLetters = async (sort = true) => {
try {
const response = await getLetters()
const logHeader = 'SafetyHome::loadLetters'
const errorMessage = 'The following error occurred when trying to load letters:'
if (Configs.SHOW_REQUEST_LOGS) console.log(`${logHeader} response`, response)
const error = hasAPIError(response, logHeader, errorMessage)
if (error) return error
const { data } = response.data.payload
let unsortedLetters = []
if (data !== null && data.length > 0) {
data.map((item) => {
// grab only unsigned letters
if (
item.assignmentStatus === SafetySources.PENDING ||
item.assignmentStatus === SafetySources.BROWSED ||
item.assignmentStatus === SafetySources.QUESTIONS_COMPLETE
) {
unsortedLetters.push({
safetyLetterId: item.safetyLetterId,
title: item.title,
assignmentStatus: item.assignmentStatus,
filePath: item.filePath,
embeddableToken: item.embeddableToken,
sponsorId: item.sponsorId,
letterDate: item.letterDate,
form16: item.form16Enabled === '1' ? true : false,
sponsorName: item.sponsorName,
type: item.letterType,
sortOrder: item.sortOrder, // dear doctor; sortOrder === 1
})
}
})
}
if (unsortedLetters.length > 0) {
let bletters = unsortedLetters.filter((letter) => letter.assignmentStatus === SafetySources.BROWSED || letter.assignmentStatus === SafetySources.QUESTIONS_COMPLETE)
console.log('useLoadLetters; setting fresh pull of letters in context, including ', bletters.length, ' browsed letters')
setUnsortedLetters(unsortedLetters) // set in context
setShowSignAll( // show/hide sign all button
unsortedLetters.some((letter) =>
letter.assignmentStatus === SafetySources.BROWSED ||
letter.assignmentStatus === SafetySources.QUESTIONS_COMPLETE,
))
}
if (sort) {
if (unsortedLetters.length > 0) {
let sortedLetters = sortLetters(unsortedLetters) // sort letters with hook
setSortedLetters(sortedLetters) // set in context
}
}
} catch (error) {
console.log('SafetyHome::loadLetters ', error)
const errorMessage = error.status && error.status.message ? error.status.message : error
Alert.alert(
'Error Loading Letters',
`A database error has occurred. Please try again. (${errorMessage})`,
)
navigation.navigate('Home')
} finally {
setModalVisible(false)
}
}
return [loadLetters]
}
signAll hook:
const { state: { unsortedLetters },
setF16Browsed,
setQcAndBrowsed,
setModalVisible,
setSelectedLetter
} = useContext(SafetyContext)
const signAll = async () => {
let qcAndBrowsed = [] // set letter groups in context
let f16Browsed = []
unsortedLetters.forEach((letter) => {
if (
letter.assignmentStatus === SafetySources.BROWSED ||
letter.assignmentStatus === SafetySources.QUESTIONS_COMPLETE
) {
if (
letter.form16 &&
letter.assignmentStatus !== SafetySources.QUESTIONS_COMPLETE
) {
f16Browsed.push(letter)
} else {
qcAndBrowsed.push(letter)
}
}
})
setQcAndBrowsed(qcAndBrowsed)
setF16Browsed(f16Browsed)
// begin sign all with first f16 letter
if (f16Browsed.length > 0) {
setSelectedLetter(f16Browsed[0])
} else {
setSelectedLetter(null) // clear any previous viewed letter
}
setModalVisible(false)
}
return [signAll]
}
I'm wrapping my forms to provide automatic validation (I don't want to use redux-form).
I want to pass an onSubmit handler which must be fired after every input in form is validated: but how do I wait for form.valid property to turn into true && after wrapping submit was fired? I'm missing some logic here!
//in my Form.js hoc wrapping the forms
#autobind
submit(event) {
event.preventDefault();
this.props.dispatch(syncValidateForm({ formName: this.props.formName, form: this.props.form }));
// ==> what should I do here? Here I know submit button was pressed but state is not updated yet with last dispatch result reduced!
//if(this.props.form.valid)
// this.props.submit();
}
render() {
return (
<form name={this.props.formName} onSubmit={this.submit}>
{ this.props.children }
</form>
);
//action.js validating given input
export const syncValidateInput = ({ formName, input, name, value }) => {
let errors = {<computed object with errors>};
return { type: INPUT_ERRORS, formName, input, name, value: errors };
};
//action.js validating every input in the form
export const syncValidateForm = ({ formName, form }) => {
return dispatch => {
for(let name in form.inputs) {
let input = form.inputs[name];
dispatch(syncValidateInput({ formName, input, name: input.name, value: input.value }));
}
};
};
//in my reducer I have
case INPUT_ERRORS:
let errors = value;
let valid = true;
let errorText = '';
_.each(errors, (value, key) => {
if(value) {
valid = false;
errorText = `${errorText}${key}\n`;
}
});
form.inputs[name].errors = errors;
form.inputs[name].valid = valid;
form.inputs[name].errorText = errorText;
_.each(form.inputs, (input) => form.valid = !!(form.valid && input.valid));
return state;
Help!
Depending on your build config you could use Async/Await for your submit function. Something like
async submit(event) {
event.preventDefault();
const actionResponse = await this.props.dispatch(syncValidateForm({ formName: this.props.formName, form: this.props.form }));
if (actionResponse && this.props.form.valid) { //for example
// finish submission
}
}
And I think you will need to update your syncValidateForm slightly but this should put you on the right path.