<?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: Gabriel</title>
    <description>The latest articles on DEV Community by Gabriel (@ratzgabriel).</description>
    <link>https://dev.to/ratzgabriel</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%2F531485%2F66f8f5fb-5c36-4860-a8c5-a1f593523985.png</url>
      <title>DEV Community: Gabriel</title>
      <link>https://dev.to/ratzgabriel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ratzgabriel"/>
    <language>en</language>
    <item>
      <title>How to create a VSC Snippet</title>
      <dc:creator>Gabriel</dc:creator>
      <pubDate>Fri, 15 Apr 2022 11:21:46 +0000</pubDate>
      <link>https://dev.to/ratzgabriel/how-to-create-a-vsc-snippet-40b0</link>
      <guid>https://dev.to/ratzgabriel/how-to-create-a-vsc-snippet-40b0</guid>
      <description>&lt;p&gt;It is very handy to use snippets in VSC such as&lt;br&gt;
"rfce" and press tab to create :&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'

function test() {
  return (
    &amp;lt;div&amp;gt;test&amp;lt;/div&amp;gt;
  )
}

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

&lt;/div&gt;



&lt;p&gt;I will show you how you can easily create your own custom snippets.For this example i want to create a user snippet which provides a react native function component&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. go to your settings and to "User snippets"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kIl6yI8t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4dkucap4knltan4cay2u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kIl6yI8t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4dkucap4knltan4cay2u.png" alt="settings-&amp;gt;user snippets" width="368" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.Create the snippet&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use double quotes to wrap the JSON properties  and Array items, but single quotes for the strings in your code snippets.&lt;/p&gt;

&lt;p&gt;Take a quick look at my example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "Function Component": {
    "prefix": "rnfc",
    "body": [
      "import React from 'react';",
      "import { View, StyleSheet } from 'react-native';",
      "",
      "function ${TM_FILENAME_BASE}(props){",
      "  return &amp;lt;View style={styles.container}&amp;gt;$0&amp;lt;/View&amp;gt;;",
      "};",
      "",
      "const styles = StyleSheet.create({",
      "  container: {},",
      "});",
      "",
      "export default ${TM_FILENAME_BASE};"
    ]
  }

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

&lt;/div&gt;



&lt;p&gt;Now lets see what we got here&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"prefix": "rnfc" This is your new shorthand&lt;/li&gt;
&lt;li&gt;with "" you can define empty lines&lt;/li&gt;
&lt;li&gt;${TM_FILENAME_BASE} creates the file with the name of the file 
-$0 will tell VSC where the cursor should be after creating the code block&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So from now on you can always just type rnfc and press tab and you will get the code block you created.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Setup Redux Step by Step Part 2</title>
      <dc:creator>Gabriel</dc:creator>
      <pubDate>Wed, 16 Mar 2022 12:37:46 +0000</pubDate>
      <link>https://dev.to/ratzgabriel/setup-redux-step-by-step-part-2-jlf</link>
      <guid>https://dev.to/ratzgabriel/setup-redux-step-by-step-part-2-jlf</guid>
      <description>&lt;p&gt;&lt;strong&gt;In Part 1 we did a basic Setup for React Redux&lt;br&gt;
using Store-&amp;gt;Reducer-&amp;gt;Actions and Action Types&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Today we will try to simplify this using Redux Toolkit.&lt;/li&gt;
&lt;li&gt;first we need to install it with "npm install @reduxjs/toolkit"&lt;/li&gt;
&lt;li&gt;What we aim to do is combine the Reducer and ActionCreators in one createSlice function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;1.) We use configureStore to simplify the store&lt;/strong&gt;&lt;br&gt;
This will make it easier to use Middleware later on, and we can easy combine reducers from different files.&lt;br&gt;
e.g /todos.js exports a reducer and /user.js exports a reducer&lt;br&gt;
&lt;/p&gt;

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

export default function configureAppStore() {
  return configureStore({
    reducer,
  });
}


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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2.) We can easy create Actions with createAction&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

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

const todoCreated = createAction('ADD_TODO');
const todoRemoved = createAction('REMOVE_TODO');
const updateStatus = createAction('CHANGE_STATUS');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create 3 actions we can dispatch 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;import configureAppStore from './store/configureStore';

const store = configureAppStore();
store.dispatch(actions.todoCreated({ description: 'Second todo' }));
store.dispatch(actions.updateStatus({ id: 1, status: 'closed' }));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3) But we can also get rid of action creators using createSlice(this will use action creators and reducers under the hood)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/todos.js
import {  createSlice } from '@reduxjs/toolkit';
const slice = createSlice({
  name: 'todos',
  initialState: [],
  reducers: {
    addTodo: (todos, action) =&amp;gt; {
      todos.push({
        description: action.payload.description,
        id: ++lastId,
        status: 'open',
      });
    },
    todoStatusChanged: (todos, action) =&amp;gt; {
      const index = todos.findIndex((todo) =&amp;gt; todo.id === action.payload.id);
      todos[index].status = action.payload.status;
    },
    todoRemoved: (todos, action) =&amp;gt; {
      todos.filter((todo) =&amp;gt; todo.id !== action.payload.id);
    },
  },
});

const { addTodo, todoStatusChanged, todoRemoved } = slice.actions;
export { addTodo, todoStatusChanged, todoRemoved };
export default slice.reducer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a function with initialState and reducer functions.It will also generate Action Creators.&lt;br&gt;
Dont forget to export the actions , also we need to &lt;br&gt;
export default slice.reducer&lt;/p&gt;

&lt;p&gt;As you can see here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  todos[index].status = action.payload.status;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we are mutating state directly, this is only possible because createSlice is using immutability under the hood.&lt;br&gt;
So it will make your code easier to read, and much more easy &lt;br&gt;
to write.&lt;/p&gt;

&lt;p&gt;I hope this explains a little bit how redux toolkit makes things easier for us and how we can use it.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Setup Redux Step by Step Part 1</title>
      <dc:creator>Gabriel</dc:creator>
      <pubDate>Tue, 15 Mar 2022 15:03:36 +0000</pubDate>
      <link>https://dev.to/ratzgabriel/setup-redux-step-by-step-38o1</link>
      <guid>https://dev.to/ratzgabriel/setup-redux-step-by-step-38o1</guid>
      <description>&lt;p&gt;These are my notes on how to setup a Redux Store for React following a tutorial from &lt;a href="https://codewithmosh.com/"&gt;https://codewithmosh.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Create Reducer&lt;/strong&gt;&lt;br&gt;
First we create a reducer, with initialState=[],the reducer could als be changed to switch if you prefer this.&lt;br&gt;
The Reducer must be export default and should never mutate state.&lt;/p&gt;

&lt;p&gt;We have 2 simple actions here&lt;br&gt;
1)addTodo&lt;br&gt;
2)removeTodo&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/reducer.js
let lastId = 0;

function reducer(state = [], action) {
  if (action.type === 'addTodo')
    return [
      ...state,
      {
        description: action.payload.description,
        id: ++lastId,
        status: 'open',
      },
    ];
  else if (action.type === 'removeTodo')
    return state.filter((todo) =&amp;gt; todo.id !== action.payload.id);
  else return state;
export default reducer;`
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2.Create Store&lt;/strong&gt;&lt;br&gt;
Here we create a store which takes our reducer.The store takes the actions and forwards them to the reducer&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/store.js
import { createStore } from 'redux';
import reducer from './reducer';

const store = createStore(reducer);

export default store;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3.Dispatching Actions&lt;/strong&gt;&lt;br&gt;
This is our first simple dispatch "addTodo"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import store from './store/store';

function App() {
  store.dispatch({
    type: 'addTodo',
    payload: {
      description: 'first Todo',
    },
  });
  return &amp;lt;div className="App"&amp;gt;Landing Page&amp;lt;/div&amp;gt;;
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4.subscribe/unsubscribe to store&lt;/strong&gt;&lt;br&gt;
Here you can subscribe to the store, the function will be executed everytime the store changes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;store.subscribe(() =&amp;gt; {
    console.log('store changed', store.getState());
this function gets called every time the store changes
  });

unsubscribe:
const unsubscribe = store.subscribe(() =&amp;gt; {
    console.log('store changed', store.getState());
  });

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5.Add Types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use Types so we do not have to hardcode them and we can change them in one single place&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/types.js
export const ADD_TODO = 'ADD_TODO';
export const REMOVE_TODO = 'REMOVE_TODO';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6.To not always have to call the actions you can use Action Creators&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as actions from './types';

export function addTodo(description) {
  return {
    type: actions.ADD_TODO,
    payload: {
      description,
    },
  };
}

export function removeTodo(id) {
  return {
    type: actions.REMOVE_TODO,
    payload: {
      id,
    },
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;7.Use Action Creators&lt;/strong&gt;&lt;br&gt;
To use the action creators we call the store dispatch with the new created actions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;store.dispatch(addTodo('First todo'));
store.dispatch(removeTodo(1));

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

&lt;/div&gt;



&lt;p&gt;In the next post i will show how to simplify this using redux toolkit. So stay tuned :)&lt;/p&gt;

</description>
      <category>redux</category>
      <category>react</category>
    </item>
  </channel>
</rss>
