Way to not repeat code in React project?

Hey guys,

I am trying to create a résumé builder application to practice React and now I am working on the ability to add multiple education and job entries. I started following the freecodecamp article below to help me but now I am stuck because I do not want to repeat the same lines of code for both for the add and remove functionality for the jobs and education section of my app. How can I just write the lines of code once and have it work for both the jobs section and education section?

Article: How to Build Dynamic Forms in React

GH: GitHub - pllealfunes/react-cv-app: A React app that creates a CV Application

GHP: React App

I don’t program in javascript but perhaps you need to create a function first, put all the code in it (that is repetitive), then modify its parameters so the code works for your jobs section only.
After that function is working, abstract it further by modifying the contents (possibly by passing in an argument array) and using these arguments in the code so that the education section can also use it (while back-testing the job section is still in good shape).

A hypothetical answer I know but even if I did code in js, I’m not sure I would be able to offer more than that.

Thanks for the help. I was able to get it to work by adding an id to each button and passing that id as a parameter on button click. Then in my function I checked for the id and depending on the id then do a certain something (if else if).

1 Like