<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>black_stone101.log</title>
        <link>https://velog.io/</link>
        <description>Frontend Developer in North !</description>
        <lastBuildDate>Wed, 04 Jan 2023 18:16:42 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <image>
            <title>black_stone101.log</title>
            <url>https://velog.velcdn.com/images/black_stone101/profile/a3271267-1212-4f3a-8042-a8cee9833009/image.jpg</url>
            <link>https://velog.io/</link>
        </image>
        <copyright>Copyright (C) 2019. black_stone101.log. All rights reserved.</copyright>
        <atom:link href="https://v2.velog.io/rss/black_stone101" rel="self" type="application/rss+xml"/>
        <item>
            <title><![CDATA[forwardRef vs useRef]]></title>
            <link>https://velog.io/@black_stone101/forwardRef-vs-useRef</link>
            <guid>https://velog.io/@black_stone101/forwardRef-vs-useRef</guid>
            <pubDate>Wed, 04 Jan 2023 18:16:42 GMT</pubDate>
            <description><![CDATA[<p>forwardRef를 알려면 먼저 useRef에 대해 할아야 한다. </p>
<p>1) ref값이 변하여도 컴포넌트를 리렌더링 하지 않는다.
2) 컴포턴드가 리렌더링를 해도 ref 값은 변하지 않고 유저가 직접 변경해야 한다.
3) ref는 HTML DOM element를 직접 조종할수있다</p>
<p><img src="https://velog.velcdn.com/images/black_stone101/post/fe90ee5d-9add-4b02-b2b5-2683e69d12b5/image.png" alt=""></p>
<p>1) ref 값이 변경해도 컴포넌트는 리렌더링 되지 않는다
위에 코드를 돌려본다면, 라인6 콘솔이 처음에만 프린트 되고, 버튼을 클릭해도 카운터 컴포넌트가 리렌더링 되지 않는것을 알수있다. 그리고 라인18에 텍스트도 <code>Clicked 0 times !</code> 로 넘버가 0에서 부터 업데이트 되지 않는것을 볼수있다. 그 이유는 컴포넌트가 ref값이 변경되어도 컴포넌트를 리렌더링 하지 않기 때문이다.</p>
<p><img src="https://velog.velcdn.com/images/black_stone101/post/a826f0ac-0d52-4230-bdf6-9ce02dbe05a1/image.png" alt=""></p>
<p>2) 컴포넌트가 리렌더링을 하여도 ref값은 초기화 되지 않고 이전 렌더링의 값을 유지한다
위에 코드는 버튼 2개를 만들어 첫번째 버튼은 ref를 두번째 버튼은 스테이트를 사용하여 버튼이 클릭될때마다 값을 1씩 증가한다. 첫번째 버튼을 3번정도 클릭하고 두번째 버튼을 클릭해서 라인18 콘솔을 보면 <code>Updating state 1 times! while ref is 3</code> 프린트된걸 볼수있다. ref의 값은 컴포넌트가 리렌더링 되어도 값이 변하지 않고 유저가 직접 변경할수 있는것을 볼수있다. 스테이트라면 직접 변경하지 못하지 못한다. 예 위에 코드에서 <code>counter = counter + 1</code> 을 하여 변경하지 못한다.</p>
<p><em>*<em>따라서 ref의 값이나 행동은 스테이트와 달리 컴포넌트와 연결되어있지 않다.
*</em></em></p>
<p><img src="https://velog.velcdn.com/images/black_stone101/post/905c8bf4-dd77-45c5-b6af-c200b88fb7a0/image.png" alt="">
3) ref는 HTML DOM element를 직접 조종할수있다
위 코드는 Form 컴포넌트가 렌더링을 끝나면 브라우저 포커스가 input element에 있는것을 볼수있다. 리액트는 이런 DOM element 조종을 허용하지 않지만 특수한 케이스를 위해 ref사용시 유저가 직접 조종할수 있는 방법도 제공하고 있는것이다.</p>
<h3 id="그럼-forwardref는-언제-쓰는걸까">그럼 forwardRef는 언제 쓰는걸까?</h3>
<p><strong>forwardRef는 ref를 자식 컴포넌트에 제공할때 사용한다. 왜냐하면 자식 컴포넌트의 HTML DOM에 접근할수 없기 때문이다</strong></p>
<p><img src="https://velog.velcdn.com/images/black_stone101/post/e725197b-13ad-4e17-a43b-03e5f03d229d/image.png" alt="">
Form 컴포넌트는 버튼과 Input 컴포넌트를 자식으로 리턴한다. 버튼이 클릭되면, Input컴포턴트에 포커스가 맞쳐지는 것이다. 이때 Form component는 Input component의 HTML DOM 에 접근하여 포커스를 맞추는 방법이 없다. 이리 하여, ref는 부모인 Form component에서 생성하여, DOM을 접근하고 싶은 자식 컴포넌트인 Input에 넣어준다. Input은 forwardRef함수로 인하여 props과 또하나의 인수를 받는다 이것이 부모 컴포턴트에서 생성되고 패스된 ref(부모에선 inputRef로 생성 하여 자식에게 ref로 패스해줌)이다. 그리고 패스된 ref를 html dom element인 <code>&lt;input/&gt;</code> 에 넣어주어 dom을 직접 조작할수 있게 하였다.</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[useCallBack 함수에 대해 알아보자]]></title>
            <link>https://velog.io/@black_stone101/useCallBack-%ED%95%A8%EC%88%98%EC%97%90-%EB%8C%80%ED%95%B4-%EC%95%8C%EC%95%84%EB%B3%B4%EC%9E%90</link>
            <guid>https://velog.io/@black_stone101/useCallBack-%ED%95%A8%EC%88%98%EC%97%90-%EB%8C%80%ED%95%B4-%EC%95%8C%EC%95%84%EB%B3%B4%EC%9E%90</guid>
            <pubDate>Wed, 04 Jan 2023 13:23:47 GMT</pubDate>
            <description><![CDATA[<p>useCallBack 함수는 useEffect 와 달리, dependency 비교를 레퍼런스로 하지 않고 밸류로 한다.</p>
<p>리액트 레포지터리에 있는 코드를 보면 알수있다.
<img src="https://velog.velcdn.com/images/black_stone101/post/5448b648-b097-4b67-a731-7f868a171a85/image.png" alt=""></p>
<p>그래서 컴포넌트가 리렌더를 하여도, 디펜던씨 리스트에 넣어진 변수들의 밸류값이 변하지 않으면 이전 함수콜에 저장된 <code>prevState[0]</code> 콜백을 리턴하여 컴포턴트가 렌더 할때마다 비싼게 계산되는 함수가 다시 계산되지 않고 전에 저장한 함수로 다시 쓰는것이다. ( <code>prevState</code> 는  <code>hook.memoizedState</code> 에서 온값이다. <code>const prevState = hook.memoizedState</code>. <code>hook.memoizedState</code> 는 무엇을 저장하는 지 알고 싶으면 라인 15을 보면 알수 있다. </p>
<p>useCallback 은 컴포넌트가 렌더할때마다 콜이 되는 useEffect와 이런점에 서 다른것을 알수 있다. useEffect는 컴포넌트가 리렌더링할때와 디펜던시 변수가 변함에 때라 콜이 되기 때문이다. </p>
]]></description>
        </item>
        <item>
            <title><![CDATA[Observable vs Promise]]></title>
            <link>https://velog.io/@black_stone101/Observable-vs-Promise</link>
            <guid>https://velog.io/@black_stone101/Observable-vs-Promise</guid>
            <pubDate>Thu, 22 Dec 2022 05:19:23 GMT</pubDate>
            <description><![CDATA[<p>Observables and Promises are both programming patterns that can be used to handle asynchronous data in JavaScript.</p>
<p><strong>Observables are a pattern for dealing with asynchronous data streams, similar to the way that arrays deal with synchronous data collections.</strong> An Observable is like a <strong>stream which can emit multiple values over time</strong>, and can also be cancelled. Observables are often used with the RxJS library in Angular applications, but they can also be used with other libraries or on their own.</p>
<p><strong>Promises, on the other hand, are a pattern for handling asynchronous operations that return a single value at some point in the future</strong>. A Promise represents the result of an asynchronous operation, and provides a way to register callbacks to be executed when the asynchronous operation completes. Promises are a built-in feature of JavaScript, and are widely used for handling asynchronous operations in many JavaScript libraries and frameworks.</p>
<p>In summary, Observables are a way to handle multiple values over time, whereas Promises are a way to handle a single value that will be available at some point in the future.</p>
<hr>
<p>In JavaScript, an observable is a type of object that can emit a stream of values over time. Observables are often used for asynchronous programming tasks, such as making network requests or handling user input.</p>
<p>A promise, on the other hand, is a special type of object that represents the result of an asynchronous operation. A promise can be in one of three states: pending, fulfilled, or rejected. When a promise is created, it is in the pending state. Once the asynchronous operation is completed, the promise is either fulfilled with a value or rejected with an error.</p>
<p><strong>One key difference between observables and promises is that observables can emit multiple values over time, while a promise can only be fulfilled or rejected once.</strong> Observables also have a wider range of operator methods available, such as map, filter, and reduce, which can be used to transform or manipulate the stream of values emitted by the observable.</p>
<p>Here is an example of using an observable to emit a stream of values:</p>
<pre><code>const observable = new Observable(subscriber =&gt; {
  subscriber.next(1);
  subscriber.next(2);
  subscriber.next(3);
  setTimeout(() =&gt; {
    subscriber.next(4);
    subscriber.complete();
  }, 1000);
});

console.log(&#39;just before subscribe&#39;);
observable.subscribe({
  next(x) { console.log(&#39;got value &#39; + x); },
  error(err) { console.error(&#39;something wrong occurred: &#39; + err); },
  complete() { console.log(&#39;done&#39;); }
});
console.log(&#39;just after subscribe&#39;);
</code></pre><p>Output:</p>
<pre><code>just before subscribe
got value 1
got value 2
got value 3
just after subscribe
got value 4
done</code></pre><p>And here is an example of using a promise to handle the result of an asynchronous operation:</p>
<pre><code>const promise = new Promise((resolve, reject) =&gt; {
  setTimeout(() =&gt; resolve(&#39;done&#39;), 1000);
});

promise.then(
  result =&gt; console.log(result),
  error =&gt; console.log(error)
);</code></pre><p>Output:</p>
<pre><code>done</code></pre>]]></description>
        </item>
        <item>
            <title><![CDATA[of]]></title>
            <link>https://velog.io/@black_stone101/of</link>
            <guid>https://velog.io/@black_stone101/of</guid>
            <pubDate>Wed, 21 Dec 2022 21:34:48 GMT</pubDate>
            <description><![CDATA[<p>Observable</p>
<p>What is of() ?</p>
<p>of() turns any plain value into an observable. 
It can be one than one types of value and any number of values.</p>
<pre><code>import { of } from &#39;rxjs&#39;

const numbers = [ 1, 2, 3, 4, 5]
const numberObservable = of(numbers)
numberObservable.subscribe(number =&gt; console.log(number))
// outputs...
// 1
// 2
// 3
// 4
// 5


const mixSource = of({ age: 14 }, [ 1 ,2, 3 ], function hello() { return &#39;Hello&#39;})
mixSource.subscribe(source =&gt; console.log(source))
// outpus...
// { age : 14 }
// [ 1, 2, 3]
// function hello() { return &#39;Hello&#39; }</code></pre><p>As you can see in the example that it doesnt run any function that is fed as a source but just spits them out from the subscription.</p>
]]></description>
        </item>
    </channel>
</rss>