<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Fahim Hossain</title>
    <description>The latest articles on DEV Community by Fahim Hossain (@alter80).</description>
    <link>https://dev.to/alter80</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F678140%2F230d7880-b589-4160-8704-674dab917ed9.jpeg</url>
      <title>DEV Community: Fahim Hossain</title>
      <link>https://dev.to/alter80</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alter80"/>
    <language>en</language>
    <item>
      <title>Welcome to the Custom Components from Styled-Components!</title>
      <dc:creator>Fahim Hossain</dc:creator>
      <pubDate>Fri, 04 Mar 2022 21:17:16 +0000</pubDate>
      <link>https://dev.to/alter80/welcome-to-the-custom-components-from-styled-components-3np0</link>
      <guid>https://dev.to/alter80/welcome-to-the-custom-components-from-styled-components-3np0</guid>
      <description>&lt;p&gt;If you are practicing React or similar JS library then sometimes its a hectic operation to make multiple divs or HTML tags. And Styling them into that HTML tags and managing them is a hard work too. &lt;/p&gt;

&lt;p&gt;To make your own custom stlyed components, "Styled-Components" is a popular solution for this type of task. &lt;/p&gt;

&lt;p&gt;*&lt;em&gt;&lt;u&gt;What is Styled-Components?&lt;/u&gt; *&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;styled-components is the result of wondering how we could enhance CSS for styling React component systems.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Some Advantages of Styled Components: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Personalized Component name&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No class name conflict&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Easy deletetaion of CSS and troubleshooting&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dynamic styling with prop values&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Basic CSS, Easy maintanece etc. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As we got the basic idea what actually it is. &lt;/p&gt;

&lt;p&gt;Before getting into the basics lets know how to install it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# with npm
npm install --save styled-components

# with yarn
yarn add styled-components
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;The Basics&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Lets dig into it. &lt;br&gt;
the basics of Styled-components format is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ComponentName = styled.h1`
    font-size: 1em; 
    color: red; 
`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This type of component creation can be done in the same file or can be also written in a separate file too. &lt;/p&gt;

&lt;p&gt;if we look into a real styling the overall view will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create a Title component that'll render an &amp;lt;h1&amp;gt; tag with some styles
const Title = styled.h1`
  font-size: 1.5em;
  text-align: center;
  color: palevioletred;
`;

// Create a Wrapper component that'll render a &amp;lt;section&amp;gt; tag with some styles
const Wrapper = styled.section`
  padding: 4em;
  background: papayawhip;
`;

// Use Title and Wrapper like any other React component – except they're styled!
render(
  &amp;lt;Wrapper&amp;gt;
    &amp;lt;Title&amp;gt;
      Hello World!
    &amp;lt;/Title&amp;gt;
  &amp;lt;/Wrapper&amp;gt;

//source: styled-components documentation

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Reuse of Existing Tags&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Also we can reuse the existing styled component like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Button = styled.button`
     color: red;
     font-size: 1em;
     margin: 1em;
     padding: 0.25em 1em;
     border: 2px solid palevioletred;
     border-radius: 3px;
`;

// A new component based on Button, but with some override styles
const TomatoButton = styled(Button)`
  color: tomato;
  border-color: tomato;

render(
  &amp;lt;div&amp;gt;
    &amp;lt;Button&amp;gt;Normal Button&amp;lt;/Button&amp;gt;
    &amp;lt;TomatoButton&amp;gt;Tomato Button&amp;lt;/TomatoButton&amp;gt;
  &amp;lt;/div&amp;gt;
);
`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Use Of Props in&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
Styled-components can have props to make similar component reusable. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Headline = styled.h1`
  color: ${props =&amp;gt; props.color};
`

function App() {
  return (
    &amp;lt;&amp;gt;
    &amp;lt;Headline color="red"&amp;gt;
      Text 
    &amp;lt;/Headline&amp;gt;
    &amp;lt;Headline color="blue"&amp;gt;
      Text 
    &amp;lt;/Headline&amp;gt;
    &amp;lt;/&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Conditional Props&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
These props are dynamic inputs, it can be useable in conditional cases.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Headline = styled.h1`
  visibility: ${props =&amp;gt; (
    props.show ? "visible" : "hidden")
  };`

function App() {
  return (
    &amp;lt;Headline show={false}&amp;gt;
      Text 
    &amp;lt;/Headline&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are primary basics of styled components. &lt;br&gt;
Also the styled Components has some advanced uses too. &lt;br&gt;
For more visit the styled components Documentation Here: &lt;a href="https://styled-components.com/docs/basics#motivation"&gt;https://styled-components.com/docs/basics#motivation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>styledcomponents</category>
      <category>beginners</category>
      <category>css</category>
    </item>
    <item>
      <title>Redux Toolkit &amp; its usage</title>
      <dc:creator>Fahim Hossain</dc:creator>
      <pubDate>Fri, 25 Feb 2022 08:54:24 +0000</pubDate>
      <link>https://dev.to/alter80/redux-toolkit-1d0p</link>
      <guid>https://dev.to/alter80/redux-toolkit-1d0p</guid>
      <description>&lt;p&gt;As a result of &lt;strong&gt;Redux Toolkit&lt;/strong&gt;, you don't have to worry about a lot of boilerplate when using Redux, making your app easier to adopt and implement Redux. The toolkit comes preloaded with the tools you might need when building a Redux app. Moreover, we can also customize the given configurations to suit our specific needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installation&lt;/strong&gt;&lt;br&gt;
@reduxjs/toolkit comes preloaded with the redux dependencies as well as some essential middlewares.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Redux Toolkit
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add @reduxjs/toolkit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Redux
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add redux 
yarn add react-redux 
yarn add redux-immutable-state-invariant 
yarn add redux-thunk 
yarn add redux-devtools-extension
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Store Creation&lt;/strong&gt;&lt;br&gt;
For Redux Toolkit, configureStore internally calls combineReducers to create the rootReducer, so that you don't have to worry about creating it manually. As well as configuring a few essential middlewares internally, it also helps you write clean and error-free code. These settings can be modified as needed according to the official documentation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Redux Toolkit
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { configureStore } from '@reduxjs/toolkit'
import filter from '...'
import movie from '...'

export default configureStore({
  reducer: {
    filter,
    movie,
  },
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Redux
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { combineReducers, applyMiddleware, createStore } from "redux"
import reduxImmutableStateInvariant from 'redux-immutable-state-invariant'
import thunk from 'redux-thunk'
import { composeWithDevTools } from 'redux-devtools-extension/logOnlyInProduction'
import filter from '...'
import movie from '...'

// Custom middlewares based on redux-immutable-state-invariant
const immutableStateInvariant = // deeply compares state values for mutations. 
// It can detect mutations in reducers during a dispatch, and also mutations that 
// occur between dispatches (such as in a component or a selector). When a 
// mutation is detected, it will throw an error and indicate the key path for 
// where the mutated value was detected in the state tree.
const serializableStateInvariant = // a custom middleware created specifically 
// for use in Redux Toolkit. Similar in concept to immutable-state-invariant, 
// but deeply checks your state tree and your actions for non-serializable values 
// such as functions, Promises, Symbols, and other non-plain-JS-data values. 
// When a non-serializable value is detected, a console error will be printed 
// with the key path for where the non-serializable value was detected.
const middleware = process.env.NODE_ENV !== 'production' ?
  [thunk, immutableStateInvariant, serializableStateInvariant] :
  [thunk];
const rootReducer = combineReducers({ 
  filter, 
  movie, 
})
export default createStore(rootReducer, composeWithDevTools(
  applyMiddleware(...middleware)
))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Creating reducers and sync actions (aka, slices)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Redux Toolkit introduces a new concept called slices, which is essentially a consolidated object containing the reducer and all the synchronous actions. No more action types or types of actions, and the state can now be mutated.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Redux Toolkit
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createSlice } from '@reduxjs/toolkit'

const sliceName = 'movie'

const movieSlice = createSlice({
  name: sliceName,
  initialState: {
    entities: [],
    totalEntities: 0,
    error: '',
    loading: false,
  },
  reducers: {
    resetMovies: (state) =&amp;gt; {
      state.entities = []
      state.totalEntities = 0
      state.error = ''
      state.loading = false
    },
  },
})

export const { resetMovies } = movieSlice.actions

export default movieSlice.reducer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Redux
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const initialState = {
  entities: [],
  totalEntities: 0,
  error: '',
  loading: false,
}

const RESET_MOVIES = 'RESET_MOVIES'

export const resetMovies = () =&amp;gt; ({
  type: RESET_MOVIES
})

export default function movie(state = initialState, action) {
  switch (action.type) {
    case RESET_MOVIES:
      return {
        entities: [],
        totalEntities: 0,
        error: '',
        loading: false,
      }
    default:
      return state
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Creating async actions (aka, thunks)&lt;/strong&gt;&lt;br&gt;
Additionally, Redux Toolkit includes the createAsyncThunk function. It gives us 3 implied sync actions for every thunk to be processed in our reducer, namely .pending, .fulfilled and .rejected. Therefore, we do not need to define actions for each state. We get 3 implied sync actions for each thunk to be processed in our reducers. These are &lt;code&gt;&amp;lt;thunkStringName&amp;gt;.pending&lt;/code&gt; , &lt;code&gt;&amp;lt;thunkStringName&amp;gt;.fulfilled&lt;/code&gt; and &lt;code&gt;&amp;lt;thunkStringName&amp;gt;.rejected&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Redux Toolkit
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'

const sliceName = 'movie'

export const fetchMovies = createAsyncThunk(
  `${sliceName}/fetchMovies`,
  (_, { getState }) =&amp;gt; {
    const { searchTerm, page, type } = getState().filter
    return movieAPI.fetchBySearch(searchTerm, page, type)
  }
)

const movieSlice = createSlice({
  ...
  extraReducers: {
    [fetchMovies.pending]: (state) =&amp;gt; {
      state.loading = true
    },
    [fetchMovies.fulfilled]: (state, action) =&amp;gt; {
      state.entities = action.payload.Search
      state.totalEntities = action.payload.totalResults
      state.error = ''
      state.loading = false
    },
    [fetchMovies.rejected]: (state, action) =&amp;gt; {
      state.entities = []
      state.totalEntities = 0
      state.error = action.error.message
      state.loading = false
    },
  },
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Redux
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...

const FETCH_MOVIES_PENDING = 'FETCH_MOVIES_PENDING'
const FETCH_MOVIES_FULFILLED = 'FETCH_MOVIES_FULFILLED'
const FETCH_MOVIES_REJECTED = 'FETCH_MOVIES_REJECTED'

...

export const fetchMoviesPending = () =&amp;gt; ({
  type: FETCH_MOVIES_PENDING
})
export const fetchMoviesFulfilled = (result) =&amp;gt; ({
  type: FETCH_MOVIES_FULFILLED,
  payload: result
})
export const fetchMoviesRejected = (error) =&amp;gt; ({
  type: FETCH_MOVIES_REJECTED,
  payload: error
})

export function fetchMovies() {
  return async function (dispatch, getState) {
    dispatch(fetchMoviesPending())
    const { searchTerm, page, type } = getState().filter
    try {
      const result = await movieAPI.fetchBySearch(searchTerm, page, type)
      dispatch(fetchMoviesFulfilled(result))
    } catch (error) {
      dispatch(fetchMoviesRejected(error))
    }
  }
}

export default function movie(...) {
  switch (action.type) {
    ...
    case FETCH_MOVIES_PENDING:
      return {
        ...state,
        loading: true,
      }
    case FETCH_MOVIES_FULFILLED:
      return {
        entities: action.payload.Search,
        totalEntities: action.payload.totalResults,
        error: '',
        loading: false,
      }
    case FETCH_MOVIES_REJECTED:
      return {
        entities: [],
        totalEntities: 0,
        error: action.error.message,
        loading: false,
      }
    ...
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Usage In React APP&lt;/strong&gt;&lt;br&gt;
Once the store and the slices have been created, you can setup Redux in your app the same way as you've always have.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import store from './store'
import { Provider } from 'react-redux'

ReactDOM.render(
  &amp;lt;React.StrictMode&amp;gt;
    &amp;lt;Provider store={store}&amp;gt;
      &amp;lt;App /&amp;gt;
    &amp;lt;/Provider&amp;gt;
  &amp;lt;/React.StrictMode&amp;gt;,
  document.getElementById('root')
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Movies/index.jsx
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect } from 'react'
import Movies from './presenter'
import { useSelector, shallowEqual, useDispatch } from 'react-redux'
import { search } from '../../services/filter/slice'

export default () =&amp;gt; {
  const { entities, totalEntities, error, loading } = useSelector(
    (state) =&amp;gt; state.movie,
    shallowEqual
  )
  const searchTerm = useSelector((state) =&amp;gt; state.filter.searchTerm)
  const dispatch = useDispatch()

  useEffect(() =&amp;gt; {
    dispatch(search(searchTerm))
  }, [dispatch, searchTerm])

  return (
    &amp;lt;Movies
      entities={entities}
      totalEntities={totalEntities}
      error={error}
      loading={loading}
    /&amp;gt;
  )
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;Sources: *&lt;/em&gt;&lt;br&gt;
_Codes are taken from this github. _&lt;br&gt;
*&lt;em&gt;Github *&lt;/em&gt;  &lt;a href="https://github.com/batbrain9392/redux-tutorial"&gt;https://github.com/batbrain9392/redux-tutorial&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>What is CRUD!</title>
      <dc:creator>Fahim Hossain</dc:creator>
      <pubDate>Fri, 24 Dec 2021 16:46:54 +0000</pubDate>
      <link>https://dev.to/alter80/what-is-crud-54e</link>
      <guid>https://dev.to/alter80/what-is-crud-54e</guid>
      <description>&lt;p&gt;CRUD is a short form of Create, Read, Update &amp;amp; Delete. These 4 steps are the process of creating, reading, updating and deleting methods in Database. These methods can be applied in both Relational or Non-Relational databases.&lt;/p&gt;

&lt;p&gt;We know databases are used to save user data, login data, product data or anything that needs to be stored for the applications. To Create a database or insert data, we need to create a database using the create method. To save the data and to read the data from the database we need to perform a Read method. Also there needs to be update and delete these data when needed. The Update and Delete are for deleting and updating the data in databases. &lt;/p&gt;

&lt;p&gt;But before starting the details, in MongoDB the datas is saved into a collection format. These collections are similar to JSON file format. Now lets move to creating and interesting data into a collection. Also to perform these operations we will use Node.Js as runtime and Express.Js as the framework to get these tasks in an easier format. &lt;/p&gt;

&lt;p&gt;In Express, Mongo, Node the CRUD operations are handled by these keywords like POST, GET, PUT and DELETE.Where&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create is POST&lt;/li&gt;
&lt;li&gt;Read is GET&lt;/li&gt;
&lt;li&gt;Update is PUT &amp;amp;&lt;/li&gt;
&lt;li&gt;Delete is DELETE&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's dig into the details. We will talk about the CRUD operation in MongoDB which is a non-relational database. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create Operations&lt;/strong&gt;&lt;br&gt;
Creating or inserting operation does the task of creating a new collection or insert or add a new data into a collection. Browsers will perform Create operation when any POST request is sent to the server. Usually POST packs the data and sends it to the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post('/test', (req, res) =&amp;gt; {
    const result = collectionName.insertOne(singleData);
res.json(result);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This above part of code creates a single data insert and if there is no collection name in the mongoDB then a collection will be opened according to the given collection name in the code. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read Operations&lt;/strong&gt;&lt;br&gt;
Reading operations means doing the GET operations. We can get a single item using findOne() method or we can get the whole data to read by using find() method. In the code we can write.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/test', (req, res) =&amp;gt; {
    const result = collectionName.find({}) //to read the whole data in an object.
    const result = collectionName.findOne(singleData);
res.json(result);

 })

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code does the reading operations between find and findOne operations, find is just used to get all the data available in the collection whereas findOne finds the specific data available in the collection. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update Operation&lt;/strong&gt;&lt;br&gt;
To update data in the database we need to use the PUT operation. But here is a new thing. First we need to find the data using its own unique ID which is _id or anything in the MongoDB and then we can set/edit the data in it. If we look into the code the code will be like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.put('/test',(req, res) =&amp;gt; {
      const filterData = { email: user.email };
      const updateData = { $set: { role: 'admin' } };
      const result = usersCollection.updateOne(filterData , updateData );
      res.json(result);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code runs a query using filter data or the data received from the request and then it updates the data in the 2nd line of the block using $set: {update data}. And then as a response it updates the data. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Delete Operation&lt;/strong&gt;&lt;br&gt;
To delete data in the database we need to use DELETE operation. The process is similar to updating the data because when it does delete a single data the data needs to be searched in the collection. In the code,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.put('/test/:id',(req, res) =&amp;gt; {
      const filterData = { _id: Object(id) };
      const result = usersCollection.deleteOne(filterData);
      res.json(result);
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code filters and gets the specific id using MongoDBs objectID method then it deletes a single item from the whole collection. &lt;/p&gt;

&lt;p&gt;That's it for crud operation basics in mongoDB. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Component Lifecycle in React</title>
      <dc:creator>Fahim Hossain</dc:creator>
      <pubDate>Wed, 22 Dec 2021 17:48:02 +0000</pubDate>
      <link>https://dev.to/alter80/component-lifecycle-in-react-16i3</link>
      <guid>https://dev.to/alter80/component-lifecycle-in-react-16i3</guid>
      <description>&lt;p&gt;*&lt;em&gt;ReactJS *&lt;/em&gt; works by using components. It makes the code more reusable. These components have a specific lifecycle where they get life and work on the code then it goes into hibernation. Before starting about the component lifecycle, we need to know what is component in ReactJS?&lt;/p&gt;

&lt;p&gt;A component is a block of a react app. Multiple components are basically blocks of the react app having a specific HTML render output in it. So we can say the starting component is App.js in the prebuilt react file. Which can be connected with the other components for routing or building the site more dynamic and useful. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mZ1A3Uux--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ojef357uos4zqouki7mw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mZ1A3Uux--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ojef357uos4zqouki7mw.png" alt="Image description" width="880" height="260"&gt;&lt;/a&gt;&lt;br&gt;
Functional and Class components are two types of components written in React, we will talk about functional components. The above image represents a functional component where a function is having a return output. This function is exportable, which means we can use this component anywhere in the react app and the function will print the line written under &lt;/p&gt; wrapper. If anyone needs to use multiple divs in a single element, then he/she can use multiple div tags under the main div tag. 

&lt;p&gt;So this is somewhat an idea about components and what a component really is in ReactJS. But we were talking about the component lifecycle. These components work in a cycle where it maintains 3 steps. They are: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Mounting&lt;/li&gt;
&lt;li&gt;Updating and&lt;/li&gt;
&lt;li&gt;Unmounting&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KjcCshWh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bqr1g06x73n3wpyvlzca.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KjcCshWh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bqr1g06x73n3wpyvlzca.PNG" alt="Image description" width="880" height="319"&gt;&lt;/a&gt;&lt;br&gt;
(source: &lt;a href="https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/"&gt;https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Mounting&lt;/strong&gt;&lt;br&gt;
In this phase of react component its being initialised by using constructors. These constructors are simple functions or methods having the state of the variables. Though this constructor applies on class based components because in Functional components it uses “useState” hook for setting and changing the state in a variable. &lt;br&gt;
So mostly the hook is there to handle these constructors. From the Q&amp;amp;A section of react it says: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y5_GwSdv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rmb81aw4wq19zxojf3rc.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y5_GwSdv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rmb81aw4wq19zxojf3rc.PNG" alt="Image description" width="880" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Anyways, so in this phase the component mounts in the DOM using a default state. In the functional component the code will be similar like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = () =&amp;gt; {
  const [counter, setCounter] = useState(0);

  console.log("This happens on EVERY render.");
  return (
    &amp;lt;&amp;gt;
      &amp;lt;div&amp;gt;Counter: {counter}&amp;lt;/div&amp;gt;
      &amp;lt;div style={{ marginTop: 20 }}&amp;gt;
        &amp;lt;button onClick={() =&amp;gt; setCounter(counter + 1)}&amp;gt;Increment&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;[credit: &lt;a href="https://dev.to/bytebodger/constructors-in-functional-components-with-hooks-280m"&gt;https://dev.to/bytebodger/constructors-in-functional-components-with-hooks-280m&lt;/a&gt; ]&lt;/p&gt;

&lt;p&gt;In the above code the useState hook mounts the variable to the dom and after that it will prepare itself for updating phase. &lt;/p&gt;

&lt;p&gt;componentDidMount() is invoked immediately after a component is mounted (inserted into the tree). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Updating&lt;/strong&gt; &lt;br&gt;
Updating happens when an existing dom is being changed or manipulated. These updates can be handled by state or props. From the above we somehow got a glimpse of state. But props is a value which is passed by another component. It's a read only property which is received like an argument in functional components. If we update the code by setting a new state the code will be like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = () =&amp;gt; {
  const [counter, setCounter] = useState(0);

  //new state update
  setCounter(5);


  return (
    &amp;lt;&amp;gt;
      &amp;lt;div&amp;gt;Counter: {counter}&amp;lt;/div&amp;gt;
      &amp;lt;div style={{ marginTop: 20 }}&amp;gt;
        &amp;lt;button onClick={() =&amp;gt; setCounter(counter + 1)}&amp;gt;Increment&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the above code, I’ve set the new setCounter value to 5 so the code will start counting from 5 instead of 0. Also the onClick function is updating the state on every click. &lt;/p&gt;

&lt;p&gt;If the variable or the state is changed then the render function happens. Before that the updating process keeps going on. &lt;br&gt;
componentDidUpdate() is invoked immediately after updating occurs. This method is not called for the initial render.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Unmounting&lt;/strong&gt; &lt;br&gt;
The component will unmounts when the component is being removed from the DOM. componentWillUnmount() is invoked immediately before a component is unmounted and destroyed. Perform any necessary cleanup in this method, such as invalidating timers, cancelling network requests, or cleaning up any subscriptions that were created in componentDidMount(). &lt;/p&gt;

&lt;p&gt;Though in react documentation it's clearly said that, “You should not call setState() in componentWillUnmount() because the component will never be re-rendered. Once a component instance is unmounted, it will never be mounted again.”&lt;/p&gt;

&lt;p&gt;Representation of react component lifecycle: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mHJHxj7G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z9udv52nm78jt2svdubv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mHJHxj7G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z9udv52nm78jt2svdubv.jpg" alt="Image description" width="680" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
