Triggering two function calls upon submission and then waiting for the useEffect hook to execute

Currently, I am facing a challenge with form validation that needs to be triggered on submit. The issue arises as some of the validation logic is located in a separate child component and is triggered through a useEffect dependency from the parent component using setTriggerValidation. Here is an outline of the scenario:

Within the submitAction, it is necessary for me to verify if isSectionValid in the parent component before proceeding with the actual submission process. This value is stored in the context that encapsulates my components and gets updated by the child component, which contains the specific fields and validation logic.

//
parent
const triggerSectionValidation = () => {
  setTriggerValidation(true)
}

const submitAction = () => {
  // check if section is valid
  // submit form data only if the above flag is valid
}

<form onSubmit={submitAction} /> 


// child
 useEffect(() => {
  // validate
  setIsSectionValid(true|false)
  }, [triggerValidation])

// render component 

The challenge I am encountering is that I cannot initiate the validation process within submitAction because the child's useEffect would be queued after the submission has been executed. Therefore, I need to trigger it prior to submitting the form. Wrapping setTriggerValidation and submitAction under the onSubmit event poses similar issues, and attempting different events on the submit button does not yield desired results. Do you believe there is a solution to this problem, or is it indicative of poor design?

Answer №1

React Form Validator is a great tool for handling validations in React forms. It automatically validates fields onChange and revalidates everything onSubmit, making it easy to avoid validation issues.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A valid ReactComponent must be returned in order to properly render in React. Avoid returning undefined, an array, or any other invalid object

While working on my react application, I came across an error that I have been trying to troubleshoot without any success. It seems like I must be overlooking something important that could be quite obvious. *Error: VideoDetail.render(): A valid ReactComp ...

What is the best way to correctly render several React components using a single index.js file?

I am facing an issue with rendering two React component classes. One class creates a counter and works fine, while the other class generates a simple string wrapped in HTML tags but does not render. I have tried various tutorials to troubleshoot this probl ...

Transferring AgGrid context in a functional React component

I have been working on a component that utilizes AgGrid to display a table, with the data sourced from a Redux selector. My goal is to include a button within a cell in the table that triggers an action based on the specific row's data. However, I a ...

$(...).parentElement is not a function - Troubleshooting a Problem with WebDriver IO and TypeScript

Alright, the objective is simple. I need to ascend from the root to obtain its parent element. Following the webdriver documentation, it should resemble something like this: it('should retrieve the class from the parent element', async () => { ...

Can someone guide me on creating a slideshow using Ionic?

Having trouble integrating a javascript/css slideshow into Ionic. Ionic does not support the use of ".style" in elements. Need assistance, below is the code: <head> <title>Slideshow</title> <style> .slides {display:none;} </styl ...

Angular firebase Error: The parameter 'result' is missing a specified type and is implicitly assigned the 'any' type

I have encountered an issue with the code I am working on and both the result and error are throwing errors: ERROR in src/app/login/phone/phone.component.ts(48,75): error TS7006: Parameter 'result' implicitly has an 'any' type. s ...

Safari iOS does not display any background for Buttons

Encountering an unusual problem with buttons on iOS devices specifically in Safari browser - the background color isn't being applied like it is on Desktop and Android devices. Here are the CSS properties for the button: export const Button = styled.b ...

Easiest method for creating a distinct identifier for a form-inputted value in React

My goal is to extract the value from the input field, generate a unique id for it, and then pass both the name and id to the addFolder() function. The object will then be added to the end of the state's folders array. My main challenge lies in creatin ...

Encountering this issue: Unable to access the property 'length' of an undefined variable

I'm currently developing an app using nuxt, vuetify 2, and typescript. Within the app, I have radio buttons (referred to as b1 and b2) and text fields (referred to as t1, t2, t3). When a user clicks on b1, it displays t1 and t3. On the other hand, w ...

Retrieve the initial value from the TextField

My website features multiple filters, including by date and duration, allowing users to easily find the information they need from a large dataset. There is also a "reset all filters" button that clears all filters and displays the full list of products. ...

Is it possible to create a React app (using create-react-app) paired with an Express backend all within a single

I am currently working on a React front-end application with an Express backend setup that is functioning smoothly in my local environment as a proxy API server. During development, the React front-end is hosted on port 3001 while the Express server is ru ...

The useSelector from @reduxjs/toolkit in Next.js is returning an undefined value

Utilizing js and @reduxjs/toolkit in my current project has resulted in an issue where the useSelector method is returning undefined values when trying to access data from the store. Below is a snippet of my reducer file: import { createSlice } from "@red ...

Error arises when attempting to pass interface props to a component in a React Typescript application

I am currently delving into the world of React js and typescript. As part of my learning process, I have created a demo application that allows users to input their name and age. The app features an ErrorModal that should pop up on the screen whenever inco ...

Having encountered peculiar behavior while trying to install npm

When attempting to install npm packages in my React Native project and running npm install, I am encountering an issue where the packages are installed with random strings appended. This unexpected behavior is causing my project to break. For example, I ...

Limit the character count for form fields in React Admin

Is it possible to restrict the number of characters a user can input into a field in a SimpleForm component within React-Admin? According to the documentation, React-Admin does not have a built-in feature for limiting character input. Although there is va ...

The essential guide to creating a top-notch design system with Material UI

Our company is currently focusing on developing our design system as a package that can be easily installed in multiple projects. While the process of building the package is successful, we are facing an issue once it is installed and something is imported ...

Angular Material (8) error code S2591: The variable 'require' is not defined in the current scope

Currently, I am attempting to record the date and time in the JavaScript console. Despite the code successfully logging the dates, an error message persists: Note: The code is functioning properly, with the dates being displayed in the console. It is only ...

Use TypeScript types to specify the types of props passed into a React component for better type safety and clarity

Struggling to extract the value passed to a prop in my react component, and use it as a type for other props within the same component. const TestPage = () => { return ( <Test tabs={[ { label: "test-label", value: " ...

The SyntaxError is triggered by the React-Native FlatList component

As a newcomer to React Native, I am facing an issue with refreshing the component to load city names stored in async storage. The error occurs when I utilize the FlatList component for the first time. The specific error message I encountered is: SyntaxE ...

The image is malfunctioning in the production environment, but functions perfectly on the localhost server

Currently, I am in the process of creating a website utilizing Next.js and Mantine. In order to incorporate my logo into the Header section, I utilized the Image component from next/image. Unfortunately, upon deployment, the image does not display as inten ...