NextJs & SurveyJS

If you got into a situation when you have to build a very complex survey application, you will find yourself looking for solutions that help you get a great result without building the entire thing from scratch.
If you google this you might get multiple solutions that might help you, like Typeform, but you'll get limited either by the pricing or but the limitation of option it provides.
One of the solutions you will get is a library called SurveyJs.
The SurveyJs issues with NextJs
One of the main issues that encounter many developers is that SurveyJs is a Browser-side library and on the other hand NextJs is using server-side rendering or static generation.
Installations
- Install SurveyJs
- Create a new file named
survey.tsxunderpages
As you notice we used dynamic import with ssr: false option provided by Nextjs to make sure our SurveyComponent is not included on server-side.
Read more about dynamic imports from Nextjs here.
- Create the
SurveyComponent
- Create the file containing the survey
questions
Question types
SurveyJs provide a variety of question types I will try to mention some.
- Radio
- Dropdown
- Percentages
- Checkbox
Widgets
There are also many widgets that you can use. You will find the list of widgets here.
But first to be able to work with these widgets you need to: install surveyjs-widgets
Since they don't support TypScript yet, you will need to create a file named
surveyjs-widgets.d.tsand include this line in it.
- Import
widgetsin theSurveyComponent
- Import widget styles in our example we will use
nouislider
- And finally, add this inside
SurveyComponent
After that, you can simply add the questions as type: nouislider, and this works for all other widgets.
Passing question value to other questions
This is very easy just like in the example below:
The same goes for other question properties like:
visibleIf: "{number} > 4"
Save survey state to local storage
To prevent losing questions answers on page reload SurveyJs provides a way of doing it, by saving survey data in local storage.
You can call this inside your SurveyComponent
Save data on survey complete
I hope you found that interesting, informative, and entertaining. I would be more than happy to hear your remarks and thoughts about this solution.
If you think other people should read this post. Tweet, share and Follow me on Twitter for the next articles.