<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by ZiyaCaylan on Medium]]></title>
        <description><![CDATA[Stories by ZiyaCaylan on Medium]]></description>
        <link>https://medium.com/@ziyacaylan?source=rss-41c92433a7c1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*VBr6Fbs-RUycd-OeTsxJIQ.jpeg</url>
            <title>Stories by ZiyaCaylan on Medium</title>
            <link>https://medium.com/@ziyacaylan?source=rss-41c92433a7c1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 02:52:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ziyacaylan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[React Context Api ile Material UI kullanarak Dark Theme Oluşturma]]></title>
            <link>https://medium.com/@ziyacaylan/react-context-api-ile-material-ui-kullanarak-dark-theme-olu%C5%9Fturma-125d806dc67d?source=rss-41c92433a7c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/125d806dc67d</guid>
            <category><![CDATA[carousel]]></category>
            <category><![CDATA[context-api]]></category>
            <category><![CDATA[material-ui]]></category>
            <category><![CDATA[material-design]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[ZiyaCaylan]]></dc:creator>
            <pubDate>Mon, 26 Dec 2022 17:10:53 GMT</pubDate>
            <atom:updated>2022-12-26T17:10:53.927Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tp2Rz7VIkuF01zUsKMOTHA.jpeg" /></figure><p>Material UI kütüphanesi ile birlikte Context Api kullanarak, artık bir çok web sayfasında kullanılan dark mode nasıl oluşturabiliriz ?</p><p>Bu soruya cevaplamadan önce “Context Api nedir ?” önce bunu bir ele alalım.</p><h3><strong>Context Api Nedir ?</strong></h3><p>Component içerisinde bir state oluşturduğumuzda, state o component içerisinde kalmaktadır. Farklı componentler üzerinden bu state e ulaşmamız gerektiğinde <strong>“<em>prop drilling”</em></strong> yaparak ilgili state e ulaşmamız gerekir. Uygulamamız büyüdükçe bu işlem içerisinden çıkılmaz bir hal almaktadır.</p><p>Bu sorunu çözmek için react bizlere bir çok state yönetim aracı sunar. Bunlardan bir tane de <strong>“<em>Context Api”</em></strong> dır. Context Api bir state yönetim aracıdır ve global state’i yönetmek için kullanabileceğimiz öğrenmesi en kolay state yönetim aracıdır.<br>React uygulamalarında state’ler prop’lar yardımıyla parent (üst) componentlardan child (alt) componentlara aktarılır. Uygulama genelinde state’nin aktarıldığı component seviyesi fazla ise yani state birden fazla iç içe component e aktarılmak isteniyorsa uygulamamız yönetilemez hale gelebilir. React Context’ler component ağacında istediğimiz veriyi prop’lar üzerinden taşımadan component ler arasında taşımayı sağlar.</p><p><strong>Context Provider</strong> oluşturduğumuz context’i kullanacak componentların erişebilmesini sağlayan, context güncellemelerinde bu componentların yeniden render olmasını sağlayan ve oluşturduğumuz context’ten türeyen bir component’tır. Bu component value isminde bir prop alır ve bu prop ile context değerleri child componentlara aktarılır. Burada Context Provider componentının context’i kullanacak componentların parent’ı olmasına dikkat edilmesi gerekir. Aksi takdirde componentlar context’e erişemezler.</p><p>Context oluşturmak için <strong><em>React.createContext()</em></strong> metodunu kullanılırız. Bu metod oluşturduğumuz context için default değer belirleyebileceğimiz bir parametre alır.</p><pre>const ThemeContext = React.createContext(&quot;light&quot;);<br><br>const UserContext = React.createContext({<br>  userName: &quot;&quot;,<br>  password: &quot;&quot;,<br> isLoggedin: false <br>});</pre><p>Yukarıdaki örnekte iki farklı context oluşturduk. Oluşturduğumuz context’i kullanabilmek için <strong>Context Provider</strong> component’a ihtiyaç duyarız.Context Provider oluşturduğumuz context’i kullanacak componentların erişebilmesini sağlayan, context güncellemelerinde bu componentların yeniden render olmasını sağlayan ve oluşturduğumuz context’ten türeyen bir component’tır. Bu component value isminde bir prop alır ve bu prop ile context değerleri child componentlara aktarılır.</p><pre>&lt;ThemeContext.Provider value={values}&gt;{children}&lt;/ThemeContext.Provider&gt;</pre><p>Oluşturduğumuz Provider ı kullanabilmek için kullanmak istediğimiz componentleri içerisine alacak şekilde sarmalamamız gerekir.</p><pre>&lt;ThemeProvider&gt;<br>   &lt;UserProvider&gt;<br>     &lt;Container /&gt;<br>   &lt;/UserProvider&gt;<br>&lt;/ThemeProvider&gt;</pre><h3>Material UI kullanarak Dark Theme Oluşturma</h3><p>Konuyu daha iyi anlayabilmek için kullanışlı bir dark Mode uygulaması yapalım.</p><p>Dark mode butonumuz Navbar üzerinde olacak ve yer tutucu olması için projeye bir carousel ekleyeceğim. İlk olarak context oluşturalım.</p><pre>import { createTheme, ThemeProvider } from &quot;@mui/material&quot;;<br>import { createContext, useMemo, useState, useContext } from &quot;react&quot;;<br><br>const ThemeContext = createContext({<br>  toggleMode: () =&gt; {},<br>  mode: &quot;light&quot;,<br>});<br><br>export const ThemeContextProvider = ({ children }) =&gt; {<br>  const [mode, setMode] = useState(&quot;light&quot;);<br><br>  const colorMode = useMemo(<br>    () =&gt; ({<br>      toggleMode: () =&gt;<br>        setMode((prevMode) =&gt; (prevMode === &quot;light&quot; ? &quot;dark&quot; : &quot;light&quot;)),<br>      mode,<br>    }),<br>    [mode]<br>  );<br>  const theme = useMemo(<br>    () =&gt;<br>      createTheme({<br>        palette: {<br>          mode: mode,        <br>      }),<br>    [mode]<br>  );<br>  return (<br>    &lt;ThemeContext.Provider value={colorMode}&gt;<br>      &lt;ThemeProvider theme={theme}&gt;{children}&lt;/ThemeProvider&gt;<br>    &lt;/ThemeContext.Provider&gt;<br>  );<br>};<br><br>export const useThemeContext = () =&gt; useContext(ThemeContext);</pre><p>Context imizi oluşturduk, Provider ı kullanarak App componentimizi sarmalayalım.</p><pre>import &quot;./App.css&quot;;<br>import CssBaseline from &quot;@mui/material/CssBaseline&quot;;<br>import { ThemeContextProvider } from &quot;./context/ThemeContext&quot;;<br><br>import Navigation from &quot;./components/Navigation&quot;;<br>import Container from &quot;./components/Container&quot;;<br><br>function App() {<br>  return (<br>    &lt;ThemeContextProvider&gt;<br>      &lt;CssBaseline /&gt;<br>      &lt;Navigation /&gt;<br>      &lt;Container /&gt;<br>    &lt;/ThemeContextProvider&gt;<br>  );<br>}<br><br>export default App;</pre><p>Navbarımız ise aşağıdaki şekilde oluşturacağız.</p><pre>import * as React from &quot;react&quot;;<br>import AppBar from &quot;@mui/material/AppBar&quot;;<br>import Box from &quot;@mui/material/Box&quot;;<br>import Toolbar from &quot;@mui/material/Toolbar&quot;;<br>import Typography from &quot;@mui/material/Typography&quot;;<br>import IconButton from &quot;@mui/material/IconButton&quot;;<br>import MenuIcon from &quot;@mui/icons-material/Menu&quot;;<br><br>//burada dark mode switchimizi içeri alıyoruz.<br>import DarkModeSwitch from &quot;./DarkModeSwitch&quot;;<br><br>const Navigation = () =&gt; {<br>  return (<br>    &lt;Box sx={{ flexGrow: 1 }}&gt;<br>      &lt;AppBar position=&quot;static&quot;&gt;<br>        &lt;Toolbar&gt;<br>          &lt;IconButton<br>            size=&quot;large&quot;<br>            edge=&quot;start&quot;<br>            color=&quot;inherit&quot;<br>            aria-label=&quot;menu&quot;<br>            sx={{ mr: 2 }}<br>          &gt;<br>            &lt;MenuIcon /&gt;<br>          &lt;/IconButton&gt;<br>          &lt;Typography variant=&quot;h6&quot; component=&quot;div&quot; sx={{ flexGrow: 1 }}&gt;<br>            Material UI Dark Theme<br>          &lt;/Typography&gt;<br><br>          {/* Burada darkMode switch butomunuzu kullanıyoruz */}<br>          &lt;DarkModeSwitch /&gt;<br><br>        &lt;/Toolbar&gt;<br>      &lt;/AppBar&gt;<br>    &lt;/Box&gt;<br>  );<br>};<br><br>export default Navigation;</pre><p>Dark Mode switchimizi ise ayrı bir component olarak tanımladım ki kod satırlarımız daha okunaklı olsun.</p><pre>import { useState } from &quot;react&quot;;<br>import FormGroup from &quot;@mui/material/FormGroup&quot;;<br>import FormControlLabel from &quot;@mui/material/FormControlLabel&quot;;<br>import Switch from &quot;@mui/material/Switch&quot;;<br>import { styled } from &quot;@mui/material/styles&quot;;<br>import { useThemeContext } from &quot;../context/ThemeContext&quot;;<br><br>const DarkModeSwitch = ({ darkMode }) =&gt; {<br>  const [checked, setChecked] = useState(false);<br><br>  const { toggleMode } = useThemeContext();<br><br>  // useEffect(() =&gt; {<br>  //   darkMode === &quot;light&quot; ? &quot;dark&quot; : &quot;light&quot;;<br>  // }, [checked]);<br><br>  const MaterialUISwitch = styled(Switch)(({ theme }) =&gt; ({<br>    width: 62,<br>    height: 34,<br>    padding: 7,<br>    &quot;&amp; .MuiSwitch-switchBase&quot;: {<br>      margin: 1,<br>      padding: 0,<br>      transform: &quot;translateX(6px)&quot;,<br>      &quot;&amp;.Mui-checked&quot;: {<br>        color: &quot;#fff&quot;,<br>        transform: &quot;translateX(22px)&quot;,<br>        &quot;&amp; .MuiSwitch-thumb:before&quot;: {<br>          backgroundImage: `url(https://medium.com/&#39;data:image/svg+xml;utf8,&lt;svg%20xmlns=&quot;http://www.w3.org/2000/svg&quot;%20height=&quot;20&quot;%20width=&quot;20&quot;%20viewBox=&quot;0%200%2020%2020&quot;&gt;&lt;path%20fill=&quot;${encodeURIComponent(%3Cbr%3E%20%20%20%20%20%20%20%20%20%20%20%20&quot;#fff&quot;%3Cbr%3E)}&quot; d=&quot;M4.2 2.5l-.7 1.8-1.8.7 1.8.7.7 1.8.6-1.8L6.7 5l-1.9-.7-.6-1.8zm15 8.3a6.7 6.7 0 11-6.6-6.6 5.8 5.8 0 006.6 6.6z&quot;/&gt;&lt;/svg&gt;&#39;)`,<br>        },<br>        &quot;&amp; + .MuiSwitch-track&quot;: {<br>          opacity: 1,<br>          backgroundColor:<br>            theme.palette.mode === &quot;dark&quot; ? &quot;#8796A5&quot; : &quot;#aab4be&quot;,<br>        },<br>      },<br>    },<br>    &quot;&amp; .MuiSwitch-thumb&quot;: {<br>      backgroundColor: theme.palette.mode === &quot;dark&quot; ? &quot;#003892&quot; : &quot;#001e3c&quot;,<br>      width: 32,<br>      height: 32,<br>      &quot;&amp;:before&quot;: {<br>        content: &quot;&#39;&#39;&quot;,<br>        position: &quot;absolute&quot;,<br>        width: &quot;100%&quot;,<br>        height: &quot;100%&quot;,<br>        left: 0,<br>        top: 0,<br>        backgroundRepeat: &quot;no-repeat&quot;,<br>        backgroundPosition: &quot;center&quot;,<br>        backgroundImage: `url(https://medium.com/&#39;data:image/svg+xml;utf8,&lt;svg%20xmlns=&quot;http://www.w3.org/2000/svg&quot;%20height=&quot;20&quot;%20width=&quot;20&quot;%20viewBox=&quot;0%200%2020%2020&quot;&gt;&lt;path%20fill=&quot;${encodeURIComponent(%3Cbr%3E%20%20%20%20%20%20%20%20%20%20&quot;#fff&quot;%3Cbr%3E)}&quot; d=&quot;M9.305 1.667V3.75h1.389V1.667h-1.39zm-4.707 1.95l-.982.982L5.09 6.072l.982-.982-1.473-1.473zm10.802 0L13.927 5.09l.982.982 1.473-1.473-.982-.982zM10 5.139a4.872 4.872 0 00-4.862 4.86A4.872 4.872 0 0010 14.862 4.872 4.872 0 0014.86 10 4.872 4.872 0 0010 5.139zm0 1.389A3.462 3.462 0 0113.471 10a3.462 3.462 0 01-3.473 3.472A3.462 3.462 0 016.527 10 3.462 3.462 0 0110 6.528zM1.665 9.305v1.39h2.083v-1.39H1.666zm14.583 0v1.39h2.084v-1.39h-2.084zM5.09 13.928L3.616 15.4l.982.982 1.473-1.473-.982-.982zm9.82 0l-.982.982 1.473 1.473.982-.982-1.473-1.473zM9.305 16.25v2.083h1.389V16.25h-1.39z&quot;/&gt;&lt;/svg&gt;&#39;)`,<br>      },<br>    },<br>    &quot;&amp; .MuiSwitch-track&quot;: {<br>      opacity: 1,<br>      backgroundColor: theme.palette.mode === &quot;dark&quot; ? &quot;#8796A5&quot; : &quot;#aab4be&quot;,<br>      borderRadius: 20 / 2,<br>    },<br>  }));<br>  return (<br>    &lt;&gt;<br>      &lt;FormGroup sx={{ margin: &quot;auto&quot; }}&gt;<br>        &lt;FormControlLabel<br>          control={<br>            &lt;MaterialUISwitch<br>              // sx={{ m: &quot;auto&quot; }}<br>              defaultChecked={checked}<br>              onClick={(e) =&gt; {<br>                setChecked((e) =&gt; !e);<br>                toggleMode();<br>              }}<br>            /&gt;<br>          }<br>        /&gt;<br>      &lt;/FormGroup&gt;<br>    &lt;/&gt;<br>  );<br>};<br><br>export default DarkModeSwitch;</pre><p>Sayfa içeriğimizde yer tutusu olması için birde container componenti içerisinde bir carousel ekledim.</p><pre>import React from &quot;react&quot;;<br>import &quot;react-responsive-carousel/lib/styles/carousel.min.css&quot;; // requires a loader<br>import { Carousel } from &quot;react-responsive-carousel&quot;;<br><br>const carouselItems = [<br>  {<br>    id: 1,<br>    imageUrl:<br>      &quot;https://images.pexels.com/photos/617278/pexels-photo-617278.jpeg?auto=compress&amp;cs=tinysrgb&amp;w=1260&amp;h=750&amp;dpr=1&quot;,<br>    title: &quot;Grey Cat&quot;,<br>  },<br>  {<br>    id: 2,<br>    imageUrl:<br>      &quot;https://images.pexels.com/photos/96428/pexels-photo-96428.jpeg?auto=compress&amp;cs=tinysrgb&amp;w=1260&amp;h=750&amp;dpr=1&quot;,<br>    title: &quot;Grey Cat&quot;,<br>  },<br>  {<br>    id: 3,<br>    imageUrl:<br>      &quot;https://images.pexels.com/photos/177809/pexels-photo-177809.jpeg?auto=compress&amp;cs=tinysrgb&amp;w=1260&amp;h=750&amp;dpr=1&quot;,<br>    title: &quot;Dog&quot;,<br>  },<br>  {<br>    id: 4,<br>    imageUrl:<br>      &quot;https://images.pexels.com/photos/96938/pexels-photo-96938.jpeg?auto=compress&amp;cs=tinysrgb&amp;w=1260&amp;h=750&amp;dpr=1&quot;,<br>    title: &quot;Grey Dog&quot;,<br>  },<br>  {<br>    id: 5,<br>    imageUrl:<br>      &quot;https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?auto=compress&amp;cs=tinysrgb&amp;w=1260&amp;h=750&amp;dpr=1&quot;,<br>    title: &quot;Horse&quot;,<br>  },<br>  {<br>    id: 6,<br>    imageUrl:<br>      &quot;https://images.pexels.com/photos/127028/pexels-photo-127028.jpeg?auto=compress&amp;cs=tinysrgb&amp;w=1260&amp;h=750&amp;dpr=1&quot;,<br>    title: &quot;Horse&quot;,<br>  },<br>  {<br>    id: 7,<br>    imageUrl:<br>      &quot;https://images.pexels.com/photos/2061057/pexels-photo-2061057.jpeg?auto=compress&amp;cs=tinysrgb&amp;w=1260&amp;h=750&amp;dpr=1&quot;,<br>    title: &quot;Horse&quot;,<br>  },<br>];<br><br>const Container = () =&gt; {<br>  return (<br>    &lt;Carousel className=&quot;img-items&quot;&gt;<br>      {carouselItems.map((item) =&gt; (<br>        &lt;div key={item.id} width=&quot;250px&quot;&gt;<br>          &lt;img src={item.imageUrl} alt={item.title} width=&quot;250px&quot; /&gt;<br>          {/* &lt;p className=&quot;legend&quot;&gt;Legend 1&lt;/p&gt; */}<br>        &lt;/div&gt;<br>      ))}<br>    &lt;/Carousel&gt;<br>  );<br>};<br><br>export default Container;</pre><p>Sonuç ise aşağıdaki resimdeki gibi olacaktır.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IeJRh0sgJVO8UtHmb6s--Q.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kcRNsN9xGEMMhqVfsi7ZYg.jpeg" /></figure><blockquote>Prejenin tamamını<strong> </strong><a href="https://github.com/ziyacaylan/mui-dark-theme-app.git"><strong>github adresimden</strong></a> ulaşabilirsiniz.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=125d806dc67d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>