<?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: Samuel Twumasi</title>
    <description>The latest articles on DEV Community by Samuel Twumasi (@samtuga1).</description>
    <link>https://dev.to/samtuga1</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%2F923722%2Fafdc50b7-0987-4192-b134-89688e7796e0.jpg</url>
      <title>DEV Community: Samuel Twumasi</title>
      <link>https://dev.to/samtuga1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/samtuga1"/>
    <language>en</language>
    <item>
      <title>Get started with React Redux using Redux Toolkit</title>
      <dc:creator>Samuel Twumasi</dc:creator>
      <pubDate>Mon, 10 Oct 2022 01:48:15 +0000</pubDate>
      <link>https://dev.to/samtuga1/get-started-with-react-redux-using-redux-toolkit-5akb</link>
      <guid>https://dev.to/samtuga1/get-started-with-react-redux-using-redux-toolkit-5akb</guid>
      <description>&lt;p&gt;Have you ever wanted to provide state to your whole app or different parts of your app?&lt;/p&gt;

&lt;p&gt;Have you ever used Context API and have suffered some disadvantages it comes with? &lt;br&gt;
Such as have a deeply nested State providers in some parts of your app. &lt;br&gt;
I know you can avoid deeply nested providers by creating one big context which stores all your app's state and creating its provider to wrap your root component, but this resolves in creating one big context which manages a lot of things and in turn becomes difficult to manage and maintain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Redux&lt;/strong&gt; is the state management tool that will come to our rescue.&lt;br&gt;
Lets get started by understand how Redux actually works.&lt;/p&gt;

&lt;p&gt;Redux works by creating one central store which contains all your states in your entire application. &lt;br&gt;
It then provides the state to your app by wrapping your root component with the Provider that comes from &lt;em&gt;react-redux.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, you would be thinking how will a component get access to the state and how they will re-build whenever some state changes in the store.&lt;/p&gt;

&lt;p&gt;You have to understand that a component must not and should not change the state in the store directly. What happens is that, there is a reducer which is responsible for mutating the state in the store. &lt;/p&gt;

&lt;p&gt;Here is a better picture&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A component triggers/dispatches some action to the reducer&lt;/li&gt;
&lt;li&gt;The reducer checks the type of action that was triggered.&lt;/li&gt;
&lt;li&gt;It(reducer) then changes/mutates the state&lt;/li&gt;
&lt;li&gt;All subscribed components listening to that state automatically update with the new state.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  NOW LETS GET INTO CODING
&lt;/h3&gt;

&lt;p&gt;Lets build a simple counter&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First make sure you have your project setup already by running &lt;code&gt;npx create-react-app my-app&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now we have to install the &lt;em&gt;react-redux&lt;/em&gt; package together with &lt;em&gt;redux toolkit&lt;/em&gt; by running &lt;code&gt;npm install @reduxjs/toolkit react-redux&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React toolkit does alot of things under hood and hides most of the complexities when using only redux&lt;/li&gt;
&lt;li&gt;React-redux also makes our life simpler by connecting components to store, subscribing to store, etc...&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now we'll create our store that will hold all our app wide state&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {  configureStore } from "@reduxjs/toolkit";

const store = configureStore();
export default store;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Now we'll provide the states of the store to our app by wrapping it to our root component, in my case around the &lt;code&gt;&amp;lt;App /&amp;gt;&lt;/code&gt; in the &lt;code&gt;index.js&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

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

import './index.css';
import App from './App';

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Now lets create our counter reducer who's job is to mutate the state of the app.
&lt;/li&gt;
&lt;/ol&gt;

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

const initialState = { counter: 0 };

export const counterSlice = createSlice({
    name: "counter",
    initialState,
    reducers: {
      increment(state) {
        state.counter++;
      },
      decrement(state) {
        state.counter--;
      },
      increaseBy5(state, action) {
        state.counter = state.counter + action.payload;
      },
    },
  });

  const counterActions = counterSlice.actions;

  export default counterActions;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets try to understand the code above.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;createSlice&lt;/strong&gt; is a function from react toolkit that is used to group states that relate to each other states.&lt;br&gt;
For example we can a slice for counter, another for authentication, themes, and many others.&lt;br&gt;
In our case we only one one slice for the counter.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We then provide our initial state that a component will initially use when rendered.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The createSlice function from the toolkit has there main properties that must be set.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;name&lt;/em&gt; - This describes the name of the slice you're creating&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;initialState&lt;/em&gt; - The initial state the slice comes with&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;reducers&lt;/em&gt; - This is an object that contains the types of functions you want to perform and depending on the dispatch action from within our component.&lt;br&gt;
It will return a new state with the updated changes. The action method receives two arguments, the state and action.&lt;br&gt;
The &lt;em&gt;state&lt;/em&gt; basically is the current state of your slice and the &lt;em&gt;action&lt;/em&gt; has a payload argument that can be used in the action types functions. &lt;br&gt;
As you can see we're receiving a payload on the &lt;code&gt;increaseBy5&lt;/code&gt; method which will be passed on from the components when dispatching an action.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;counterActions - we will use them to dispatch the type of actions from our components&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Now we will add the counter slice to our store by targeting the reducer property hooked to it
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { counterSlice } from "../slices/counter-slice";

const store = configureStore({
  reducer: counterSlice.reducer,
});
export default store;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Finally we can access and dispatch actions from our components
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import classes from './Counter.module.css';
import { useSelector, useDispatch } from 'react-redux';
import counterActions from '../slices/counter-slice';

const Counter = () =&amp;gt; {

  const Counter = useSelector(state =&amp;gt; state.counter)
  const dispatch = useDispatch();

  function increase() {
    dispatch(counterActions.increment());
  }

  function decrease() {
    dispatch(counterActions.decrement());
  }

  function increaseBy5() {
    dispatch(counterActions.increaseBy5(5));
  }

  return (
    &amp;lt;main className={classes.counter}&amp;gt;
      &amp;lt;h1&amp;gt;Redux Counter&amp;lt;/h1&amp;gt;
      {showState &amp;amp;&amp;amp; &amp;lt;div className={classes.value}&amp;gt;{Counter}&amp;lt;/div&amp;gt;}

      &amp;lt;div&amp;gt;
        &amp;lt;button onClick={increase} &amp;gt;
          increment
        &amp;lt;/button&amp;gt;
        &amp;lt;button onClick={increaseBy5} &amp;gt;
          increment by 5
        &amp;lt;/button&amp;gt;
        &amp;lt;button onClick={decrease} &amp;gt;
          decrement
        &amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;

    &amp;lt;/main&amp;gt;
  );
};

export default Counter;

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

&lt;/div&gt;



&lt;p&gt;Lets understand the code above.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;useSelector - This is used to selete the part of state from our global state we want to use. By using this hook in the component, redux automatically subscribes that components to listen to changes and re-build in case that state changes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useDispatch - This is used to dispatch actions from the reducers we created before in the slice&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now whenever our functions (&lt;code&gt;increase, decrease, and increaseBy5&lt;/code&gt;) gets called, we use our dispatch together with counterActions we exported from the counterSlice to make the reducers update the state and then since the component in listening the changes in the state, it automatically re-builds&lt;br&gt;
NB: Note that increaseBy5 passes 5 to the increaseBy5 reducer, and that is the payload with which we increase our value by.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;🎯 You can always drop a comment below 😉&lt;/p&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>javascript</category>
    </item>
    <item>
      <title>INTEGRATE IMAGE PICKER SERVICE IN YOUR FLUTTER PROJECT</title>
      <dc:creator>Samuel Twumasi</dc:creator>
      <pubDate>Wed, 21 Sep 2022 15:55:58 +0000</pubDate>
      <link>https://dev.to/samtuga1/integrate-image-picker-service-in-your-flutter-project-5g50</link>
      <guid>https://dev.to/samtuga1/integrate-image-picker-service-in-your-flutter-project-5g50</guid>
      <description>&lt;h2&gt;
  
  
  Add the image picker plugin in your pubspec.yaml file
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;There are mainly two ways for adding a package to your project.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  First Approach (Long approach)
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Head over to &lt;a href="https://pub.dev/" rel="noopener noreferrer"&gt;pub.dev&lt;/a&gt; and search for &lt;strong&gt;image_picker 0.8.5+3&lt;/strong&gt; &lt;a href="https://pub.dev/packages/image_picker" rel="noopener noreferrer"&gt;Link here&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on installing and copy the image picker version &lt;em&gt;image_picker: ^(current version number)&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to your pubspec.yaml file and paste what you have copied under dependencies and make sure it is properly aligned.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hit save or run &lt;em&gt;flutter pub get&lt;/em&gt; to install all dependencies.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Alternate method (Shorter approach)
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Head over to your terminal and run &lt;strong&gt;flutter pub add image_picker&lt;/strong&gt; to add the image picker plugin to your project. As simple as it can be 😀&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Implementation of the image picker plugin
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In the state of your class (StatefulWIdget), declare a nullable variable &lt;code&gt;File? imageUrl;&lt;/code&gt; that will hold the file image. I will explain the reason for making your variable null very shortly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now we have to write a function for the image picker.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void selectImage() async {
    final picker = ImagePicker(); //Initialize the image picker service
    final pickedImage = await picker.pickImage();
    if (pickedImage == null) { //Check if the user did not pick any image, then we return.
      return;
    }
   setState((){
     imageUrl = File(pickedImage!.path); //Set imageUrl to the to File of the picked image.
   });
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the &lt;code&gt;.pickImage()&lt;/code&gt; method has some number of properties you can set, including imageQuality, source and many more.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Now head over to where you want to display your image.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Container(
  height: 200,
  width: 200,
  child: FileImage(imageUrl ?? 'any dummy image url here');//This line checks if imageUrl is null, then it displays the dummy image url else it will display your image. This is where the power or dart nullable comes in 😀.
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>flutter</category>
      <category>dart</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
