250x250
Notice
Recent Posts
Recent Comments
Link
반응형
개발세발
React 궁금증 본문
728x90
반응형
SMALL
1. useEffect 사용시 의존성 배열에 따라서 다른 console.log를 찍고 싶다면 ?
useEffect(() => {
console.log("렌더링이 완료되었습니다 ");
// console.log({ names });
if (names.includes("안녕")) {
console.log("반갑습니다");
}
return () => {
console.log("clean up");
console.log({ names });
};
}, [names, text]);
-의존성 배열에 넣은 값을 기준으로 조건문을 넣으면 의존성 배열값이 바뀔때마다 바뀐 값에 따라 다른 값을 얻을 수 있다.
2. useState와 useRef를 어떻게 구분해서 사용하는 것이 현명할까 ?
useState는 값이 변경될 때마다 렌더링이 되고
useRef는 값이 변경되어도 렌더링이 되지 않아서 보여지는 값도 바뀌지 않는다
➡️ useState는 실시간으로 값이 변경되는 것이 '화면'에서 보여져야 한다면 사용
- useRef는 값이 최종적으로 한번에 일괄적으로 바뀌니깐 input에 값을 다 입력받고 한 번에 값을 넘겨주는 경우 등에 사용하면 좋지 않을까
3. context
context 사용하다 그냥 안에 어떻게 생겼는지 궁금해서 콘솔에 찍어보니
아래와 같은 결과가 나와서 단순 기록용으로 코드 남김
function useContext(Context) {
var dispatcher = resolveDispatcher();
{
// TODO: add a more generic warning for invalid values.
if (Context._context !== undefined) {
var realContext = Context._context; // Don't deduplicate because this legitimately causes bugs
// and nobody should be using this in existing code.
if (realContext.Consumer === Context) {
error('Calling useContext(Context.Consumer) is not supported, may cause bugs, and will be ' + 'removed in a future major release. Did you mean to call useContext(Context) instead?');
} else if (realContext.Provider === Context) {
error('Calling useContext(Context.Provider) is not supported. ' + 'Did you mean to call useContext(Context) instead?');
}
}
}
return dispatcher.useContext(Context);
}
function createContext(defaultValue) {
// TODO: Second argument used to be an optional `calculateChangedBits`
// function. Warn to reserve for future use?
var context = {
$$typeof: REACT_CONTEXT_TYPE,
// As a workaround to support multiple concurrent renderers, we categorize
// some renderers as primary and others as secondary. We only expect
// there to be two concurrent renderers at most: React Native (primary) and
// Fabric (secondary); React DOM (primary) and React ART (secondary).
// Secondary renderers store their context values on separate fields.
_currentValue: defaultValue,
_currentValue2: defaultValue,
// Used to track how many concurrent renderers this context currently
// supports within in a single renderer. Such as parallel server rendering.
_threadCount: 0,
// These are circular
Provider: null,
Consumer: null,
// Add these to use same hidden class in VM as ServerContext
_defaultValue: null,
_globalName: null
};
context.Provider = {
$$typeof: REACT_PROVIDER_TYPE,
_context: context
};
var hasWarnedAboutUsingNestedContextConsumers = false;
var hasWarnedAboutUsingConsumerProvider = false;
var hasWarnedAboutDisplayNameOnConsumer = false;
{
// A separate object, but proxies back to the original context object for
// backwards compatibility. It has a different $$typeof, so we can properly
// warn for the incorrect usage of Context as a Consumer.
var Consumer = {
$$typeof: REACT_CONTEXT_TYPE,
_context: context
}; // $FlowFixMe: Flow complains about not setting a value, which is intentional here
Object.defineProperties(Consumer, {
Provider: {
get: function () {
if (!hasWarnedAboutUsingConsumerProvider) {
hasWarnedAboutUsingConsumerProvider = true;
error('Rendering <Context.Consumer.Provider> is not supported and will be removed in ' + 'a future major release. Did you mean to render <Context.Provider> instead?');
}
return context.Provider;
},
set: function (_Provider) {
context.Provider = _Provider;
}
},
_currentValue: {
get: function () {
return context._currentValue;
},
set: function (_currentValue) {
context._currentValue = _currentValue;
}
},
_currentValue2: {
get: function () {
return context._currentValue2;
},
set: function (_currentValue2) {
context._currentValue2 = _currentValue2;
}
},
_threadCount: {
get: function () {
return context._threadCount;
},
set: function (_threadCount) {
context._threadCount = _threadCount;
}
},
Consumer: {
get: function () {
if (!hasWarnedAboutUsingNestedContextConsumers) {
hasWarnedAboutUsingNestedContextConsumers = true;
error('Rendering <Context.Consumer.Consumer> is not supported and will be removed in ' + 'a future major release. Did you mean to render <Context.Consumer> instead?');
}
return context.Consumer;
}
},
displayName: {
get: function () {
return context.displayName;
},
set: function (displayName) {
if (!hasWarnedAboutDisplayNameOnConsumer) {
warn('Setting `displayName` on Context.Consumer has no effect. ' + "You should set it directly on the context with Context.displayName = '%s'.", displayName);
hasWarnedAboutDisplayNameOnConsumer = true;
}
}
}
}); // $FlowFixMe: Flow complains about missing properties because it doesn't understand defineProperty
context.Consumer = Consumer;
}
{
context._currentRenderer = null;
context._currentRenderer2 = null;
}
return context;
}
728x90
반응형
'코딩공부 > React' 카테고리의 다른 글
리액트에 아이콘 넣기 (font-awesome, react-icons) (0) | 2023.03.21 |
---|---|
react하면서 만난 오류들 (0) | 2022.12.08 |
SPA, React router (0) | 2022.12.03 |
리액트 - 3️⃣ (feat. 리액트를 다루는 기술) (0) | 2022.12.02 |
React - 2️⃣ (한입 크기로 잘라먹는 리액트) (0) | 2022.11.24 |