Shibin Khan

2 years ago · 2 min. reading time · ~100 ·

Blogging
>
Shibin blog
>
What is React & React Router & How to use CSS in react?

What is React & React Router & How to use CSS in react?

What is React Router?

ReactJS is an open-source, component-based front-end library responsible only for the view layer of the application. It is maintained by Facebook. ReactJS uses a virtual DOM-based mechanism to fill in data (views) in HTML DOM. The virtual DOM works fast owning to the fact that it only changes individual DOM elements instead of reloading complete DOM every time.

A React application is made up of multiple components, each responsible for outputting a small, reusable piece of HTML. Components can be nested within other components to allow complex applications to be built out of simple building blocks. A component may also maintain an internal state - for example, a TabList component may store a variable corresponding to the currently open tab.

React allows us to write components using a domain-specific language called JSX. JSX allows us to write our components using HTML, whilst mixing in JavaScript events. React will internally convert this into a virtual DOM, and will ultimately output our HTML for us.

React "reacts" to state changes in your components quickly and automatically to rerender the components in the HTML DOM by utilizing the virtual DOM. The virtual DOM is an in-memory representation of an actual DOM. By doing most of the processing inside the virtual DOM rather than directly in the browser's DOM, React can act quickly and only add, update, and remove components that have changed since the last render cycle occurred.

What is React Router?

When we build a website, most of them built with React.js are SPA (single page application), but it doesn’t mean your app will have only one view. It means your app doesn’t need to reload to another view, but how can we change views and go to the next page? React Router helps us to do that!

React Router is the official and standard routing package that we use in React.js to change views, and move between pages. With the React router, we can specify the whole routing for our modules that will decide what view should be visible when we enter the specified URL but not only.

React router gives us the possibility to create protected views like, for example, the view that we need to be logged in or have any special requirements to visit. One more useful feature of the React Router is the routing history, which can keep all of the histories of our views, and come back to the specified step when needed.

I couldn’t forget to tell about handling the URL params like, for example, the id of element, to render the route that can show specified elements, and give you access to that param. We can use routing navigation in a few ways. The most popular is to type URL, visit URL by a link inside the menu, or add data to the routing history.

Lets see some example:

[E443
<Route path="/about">
<About />
</Route>
<Route path="/contact/:id">
<Contact />
</Route>
<Route path="/contact">
<AllContacts />
</Route>
<Route path="/">
[LL LAS
</Route>
</Switch>

 

How To Import CSS In ReactJS?

Now I’ll show you how to import CSS in react.js components, in the two most easy, and kinda popular ways. When we build apps in React.js in some cases, we can use CSS frameworks like bootstrap, Bulma, or something popular now. Next, we can build a layout like puzzles and focus only on things like Javascript or Typescript logic. But sometimes, we need to create our own styling even I’d say that can happen kind of often, and how can we solve that?

There are a few methods of including CSS into the React.js components, some of them are more complicated, some less, but all of them have pros and cons. Anyway, today I’ll show you one of them, the easiest one, and the quickest to implement. I like to use that method, especially in bigger projects, when we need to have all the styles sorted and put into separate folders.

We can import the sass file into our files by using a relative import path. With that method, we can create a directory “styles” inside our project directory, sort all of the style files by feature, function, or how we want, and import the file into our component directly. It helps us sort all the helpers, variables, and necessary style up to files, where it’s needed, but still can be kept in one place.
 

 

Comments

Articles from Shibin Khan

View blog
2 years ago · 2 min. reading time

What is Node? · Node is a JavaScript environment built on the same JavaScript engine used in Google’ ...

You may be interested in these jobs

  • Paragon Income Tax Consultancy Ltd.

    Paragon is Hiring a Delivery Man

    Found in: beBee S2 BD - 3 days ago


    Paragon Income Tax Consultancy Ltd. Paltan, Bangladesh Full time

    ডেলিভারি ম্যান ৭ জন · বেতন আলোচনা সাপেক্ষে · অফিস শুরু 9 টায় বন্ধ ৭ টায় · যোগ্যতা: এসএসসি হইতে অনার্স · বয়স :১৮ হইতে ৩৫ · অফিসের ডকুমেন্ট গ্রাহকের কাছে পৌঁছাতে হবে · দায়িত্বশীল হতে হবে / ...

  • Aysha Enterprise

    Delivery Man for Distribution House

    Found in: beBee S2 BD - 6 days ago


    Aysha Enterprise Mirpur, Bangladesh Full time

    কাজের ধরন ‍ও দায়িত্বসমূহঃ · *পণ্যঃ ফুড এন্ড বেভারেজ, কয়েল, চা-পাতা ইত্যাদি। · *ডেলিভারী মাধ্যমঃ ভ্যান। · *ডেলিভারির স্থানঃ বিভিন্ন খুচরা দোকান। · *এরিয়াঃ মিরপুর-১ এবং গাবতলী · *কোম্পানির এস আর কর্তৃক অর্ডারকৃত ব্যতীত নিজ কোম্পনির অন্য কোন পণ্য বিক্রয়ের উপর ২০% কমিশন থাকবে (মা ...

  • TeacherOn

    Chemistry, Mathematics home tutor

    Found in: beBee S2 BD - 4 days ago


    TeacherOn Gulshan, Bangladesh Part time

    Teacher or student who is completed BSc honours ( Chemistry / Mathematics) or engineering . · For chemistry & Mathematics 10th class · Level: Masters/Postgraduate · Gender Preference: None · Meeting options: · At home - Student can meet at their place. ...