0

I have a form and I want the onSubmit function to contain a switch or an if that routes to different pages depending on form submit.

For example consider the following onSubmit function body: (but where goTo is an executable JS function that routes to /account, /blog, etc.)

'''
if (formData == 1) {
  goTo('/account');
}
else if (formData == 2) {
  goTo('/blog');
}
else if (formData == 3) {
  goTo('/about');
}
else {
  goTo('/');
}
'''

I am using next react next link and react-router. If you know of any libraries that would be useful for this please let me know.

  • 3
    Does this answer your question? [Programmatically navigate using React router](https://stackoverflow.com/questions/31079081/programmatically-navigate-using-react-router) – mcy Apr 28 '21 at 23:51
  • What have you tried so far? Have you read any documentation about doing this in React, Next, etc.? – blong Apr 29 '21 at 00:26

2 Answers2

0

Since you are using react-router, you will want to use something from the appropriate from the library.

It appears this question provides plenty of solutions: Programmatically navigate using React router

mcy
  • 281
  • 1
  • 7
0

In Next.js you can create paths using your filesystem and [foo].js filenames.

For instance, you could create the file /[part1]/[part2].js, and it would match any URL in the form /anything/anything`.

See the following for further info: https://nextjs.org/docs/routing/introduction

See also: https://nextjs.org/docs/routing/dynamic-routes (particularly the section at bottom about "Catch all routes").

machineghost
  • 33,529
  • 30
  • 159
  • 234