In 2016, I was working to build a platform to help NGOs raise donations. The platform was supposed to be built using React. The beautiful designs were in place. I was excited to build some new features as well as to try out the new architecture using Redux on a larger scale.
Yes, this was the time when Redux was fairly new. React still had PropTypes package attached to its core. The lifecycle method componentWillReceiveProps used to dominate the scene.
Out of the many design components that I worked while building that platform, I am going to discuss my first search component in React here.
Elements of a search bar
The search bar was a simple input field placed at the middle of the main header. The design idea was to have a search component which displays results as soon as the user inputs something in it. Then, to provide a simple cross icon at the right end of the input field to clear the inputs and hide the search results.
The search results were supposed to appear inside a modal starting below the main header of the site. Below are the designs to help you visualise things better:
If you typed on the search bar, a search results component appeared and showed NGOs, Live Projects and Campaigns.
Development
Redux Architecture
While building apps using Redux architecture, one should be cognizant of its three principles (Single source of truth, State is read-only and Changes are made with pure functions).
In computer programming, a pure function is a function that has the following properties:
Its return value is the same for the same arguments
Its evaluation has no side effects
In our case, a single module (file) was created to include action types, reducers and action creators. The flow was - when the user inputs something in the search field, we will take that value and pass it through an action creator which will fetch the results from the server for the queried string and return an action type along with the result. There was also a service layer in the middle, which handled the caching and server errors.
Based on the action type, our reducer will update the main store’s state with the response data and an information key to show search results modal. Hence, as soon as the store was updated, the views which were subscribed to it will render again and the search container will be added to the DOM to display the results.
Here is the module code for the Search component:
React Components
Note: I have stripped down unrelated code for a better clarity.
The dumb one Disclaimer: All the code shared in this blog was written in 2016. Be mindful when using them.Do not categorise yourself under the same category as the below input component. :P
Input
onRightIconClick prop is expected so as to perform clearing of search input.
The smart ones
Once a developer named Dan Abramov wanted to hire a smart guy to speed up the development of the React library. However, weeks went by and he had no success. Hence, he started naming his components as the smart ones.
SearchInput
SearchResults
Note: That Dan Abramov story is made-up. Do not believe everything that you read on the internet.
Header: It was divided into 3 parts viz. the logo, the search field and the links container.
These were sufficient to get the search bar up and rolling.
Parting notes
I wanted to share the code to help new developers who want to try out their hands in building a basic Search component as well as learn about the evolution of the code patterns. Certain naming conventions and methods do not hold true today. However, feel free to modernise and improve this implementation as a side project.
I also wanted to take this opportunity to inform people that there is a website which transfers 100% of your donations to the NGOs.
To people who do not code: “Many many lorem ipsum of the day. May your life be full of Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque consequat eleifend justo vitae facilisis. Praesent ut felis in velit feugiat accumsan”.