λλ λ§μ λ§₯λ½μ κ°μ§κ³ μκ³ λλ μ΄λ° μμΌλ‘ μ¨μΌλ§νλ€. μ§κΈ λ΄ μμ μ λ°©ν΄ν©λλ€. μ΄λ¬ν λμμΈμ κ±°μ μ¬μ©ν μ μκ² λ§λλλ€.
<context1.Provider value={value1}>
<context2.Provider value={value2}>
<context3.Provider value={value3}>
<context4.Provider value={value4}>
<context5.Provider value={value5}>
</context5.Provider>
</context4.Provider>
</context3.Provider>
</context2.Provider>
</context1.Provider>
<context1.Consumer>
{value1 => <context2.Consumer>
{value2 => <context3.Consumer>
{value3 => <context4.Consumer>
{value4 => <context5.Consumer>
{value5 => (
null
)}
</context5.Consumer>}
</context4.Consumer>}
</context3.Consumer>}
</context2.Consumer>}
</context1.Consumer>
곧 μΆμ λ Hooks APIλ 컨ν μ€νΈλ₯Ό μλΉνλ λ€λ₯Έ λ°©λ²μ μ 곡ν©λλ€.
κ°μ¬ν©λλ€. νμ§λ§ 곡κΈμλ μ΄λ»μ΅λκΉ?
μμ§ν λ§ν κ². μ΄λ° μ’ λ₯μ ꡬνμ΄ λ°μνλ©΄ μν€ν μ² λμμΈμ΄ μ’μ§ μμ κ² κ°κ³ React 컨ν μ€νΈλ₯Ό μ¬μ©νμ§ μμμΌ ν κ²μ λλ€.
μλμ, μ μ μ₯μ 컨ν
μ΄λλ₯Ό λμμΈνκ³ μμ΅λλ€. λ°μμμ 컨ν
μ€νΈμ ν¨κ» μλν΄μΌν©λλ€.
https://github.com/rabbitooops/rako
κ·Έ λΌμ΄λΈλ¬λ¦¬λ 곡κΈμ / μλΉμμ 5 κ³μΈ΅ κΉμ΄μ μ΄λ€ κ΄λ ¨μ΄ μμ΅λκΉ?
Reduxμ κ°μ λ¨μΌ μ μ₯μ μ루μ λμ λ°μ ν μ¬λ¬ μ μ₯μ μ£Όμ μ μ§μνκΈ° λλ¬Έμ λλ€.
μ΄ κ²½μ° μ»¨ν μ€νΈ μ²λ¦¬λ μ΄μ λΌμ΄λΈλ¬λ¦¬μ μ¬μ©μκ° μλλΌ λΌμ΄λΈλ¬λ¦¬μ μ¬μ©μμκ² μμ΅λλ€. κΈ°λ₯μ νμ©νλ λ°©λ²μ κ·Έλ€μκ² λ¬λ € μμΌλ©°, λͺ¨λ 곡κΈμλ₯Ό νκ³³μμ μν κ²½μ° (μ¬λ¬ μμ μ κ°λ λͺ©μ μ μλ°°λ¨) κ·Έκ²μ΄ κ·Έλ€μ μ νμ λλ€. μ΄μμ μΌλ‘λ λ€μ€ μ€ν μ΄ μ루μ μ΄ μ ν리μΌμ΄μ μ μ¬λ¬ λΆν μμ ꡬνλλ―λ‘ μ΄μ κ°μ μ€μ²© 컨ν μ€νΈλ ν¨μ¬ λ λλ λλ€.
μ μ΄λ λ΄ 2 μΌνΈ.
λ°λΌμ 컨ν μ€νΈ APIλ reduxμ κ°μ λ¨μΌ μ μ₯μ μ루μ μλ§ μΉμν©λλ€.
μ ν. κ·Έλ¬λ λ νμ€μ μΈ μμμ΄ λ¬Έμ λ₯Ό λ Όμνλ κ²λ μ΄λ ΅μ΅λλ€. νλ λ§λμκ² μ΅λκΉ?
μΈ κ°μ μμ ν λ§, μ¬μ©μ λ° μΉ΄μ΄ν°κ° μλ€κ³ μμν΄λ³΄μμμ€.
function theme(getState) {
return {
color: 'white',
setColor(color) {
this.setState({color})
}
}
}
function user(getState) {
return {
name: '',
setName(name) {
this.setState({name})
}
}
}
function counter(getState) {
return {
value: 0,
increment() {
const {value} = getState()
this.setState({value: value + 1})
}
}
}
const [themeStore, userStore, counterStore] = createStores(theme, user, counter)
const [themeContext, userContext, counterContext] = createContexts(themeStore, userStore, counterStore)
class App extends React.Component {
render() {
return (
<themeContext.StoreProvider>
<userContext.StoreProvider>
<counterContext.StoreProvider>
<Child />
</counterContext.StoreProvider>
</userContext.StoreProvider>
</themeContext.StoreProvider>
)
}
}
class Child extends React.Component {
static contextType = [themeContext, userContext]
render() {
const [theme, user] = this.context
/* ... */
}
}
μ΄μμ μΈ κ΅¬λ¬Έμ 무μμ λκΉ?
context.writeλ₯Ό μ¬μ©νκ³ μ€μ²©μμ΄ μ¬λ¬ 컨ν μ€νΈ μ¬μ©μ μ§μν©λλ€.
μ€μ²©μμ΄ μ¬λ¬ 컨ν μ€νΈλ₯Ό μ¬μ©νλ κ²μ μ΄λ―Έ μ§μλ©λλ€. (νν¬ ν¬ν¨.)
Context.writeμλ RFCκ° μ΄λ € μμ΅λλ€. λ§€μ° λ³΅μ‘ν μ§λ¬Έμ μ κΈ°νκΈ° λλ¬Έμ ν΅κ³Όν μ§ μ¬λΆλ μ μ μμ΅λλ€. κ·Έλ¬λ RFCκ° μ΄λ €μλ λμμ΄ λ¬Έμ μμ μ΄λ€ μ‘°μΉλ₯Ό μ·¨ν μ μλμ§ μ λͺ¨λ₯΄κ² μ΅λλ€. μ΄λ―Έ RFC λκΈ°μμλ κ² μΈμ μΆκ° ν κ²μ΄ μμ΅λκΉ?
μ§λ¬Ένκ³ μΆμ΄μ. Reactκ° ν΄λμ€μμ μ¬λ¬ 컨ν μ€νΈ μ¬μ©μ μ§μνμ§ μλ μ΄μ λ 무μμ λκΉ? Hooks APIλ ν΄κ²°ν΄μΌ ν λ¬Έμ κ° λ§κ³ νμ¬ λ§€μ° λΆμμ ν κ² κ°μ΅λλ€.
static contextType = [themeContext, userContext]
const [theme, user] = this.context
μ¬λ¬ 컨ν
μ€νΈλ₯Ό μλμΌλ‘ μ€μ²© ν μμλ StoreProviders
λ₯Ό ꡬννκ² μ΅λλ€.
const StoreProviders = constructStoreProviders(...storeContexts)
<StoreProviders>
<Child />
</StoreProviders>
λμ μ£Όμ μ κ°μ¬ν©λλ€ Dan! :)
@rabbitooops νν¬μ μ νν μ΄λ€ λ¬Έμ κ° μμ΅λκΉ? λλ νλ‘λμ μμ νν¬λ₯Ό μ¬μ©νλ©° μ°λ¦¬ νμ μ μλν©λλ€.
κ·Έλ¦¬κ³ μ΄κ²μ μ΄λ»μ΅λκΉ? μ§κΈμ νν¬λ₯Ό μ¬μ©ν΄λ μμ ν©λλ€. @gaearon
// A library
function useStoreProviders(Component, ...contexts) {
contexts.forEach(context => Component.useProvider(context, someValue))
}
// User code
function App(props) {
const [theme] = useState('white')
// Safe to use `component hook`.
App.useProvider(themeContext, theme)
App.useShouldComponentUpdate(() => {})
// Meanwhile, library can also use `component hook`.
useStoreProviders(App, storeContext1, storeContext2, storeContext3)
// Normal hook can't use `component hook`.
customHook()
/* ... */
}
<App />
decorateBeforeRender(App)
@rabbitooops https://github.com/reactjs/rfcs/issues/101
@rabbitooops λ¨μΌ μ€ν μ΄μ μ¬λ³Όμ ν€λ‘ μ¬μ©νμ¬ μ¬λ¬ μΈ΅μ μ€ν μ΄λ₯Ό λͺ¨λ°©νλ κ²μ μ΄λ»μ΅λκΉ?
data Store = Leaf Object | C Store Store
λλ μλ° μ€ν¬λ¦½νΈμμ λΆμμ ν λ°©μμΌλ‘ :
const LEFT = Symbol('LEFT')
const RIGHT = Symbol('RIGHT')
function createLeafStore = return new Store({});
function createStore(leftChild :: Store, rightChild :: Store) {
return new Store({[LEFT]: leftChild, [Right]: rightChild})
}
@TrySound λ€λ¦ : App.useProvider
@zhujinxuan μ£μ‘ν©λλ€.
@zhujinxuan λΉμ μ μ¬μ©ν μ μμ΅λλ€ μΈκΈλμ§ μμ , μλ₯Ό :
<Subscribe to={[AppContainer, CounterContainer, ...]}>
{(app, counter, ...) => (
<Child />
)}
</Subscribe>
Hooks APIλ ν΄κ²°ν΄μΌ ν λ¬Έμ κ° λ§κ³ νμ¬ λ§€μ° λΆμμ ν κ² κ°μ΅λλ€.
μ°λ¦¬λ 1 ~ 2 μ£Ό μ΄λ΄μ μΆμλ₯Ό μ€λΉνκ³ μμ΅λλ€. μ¬λ¬λΆμ΄ κ·Έκ²μ μΆλ‘ ν μ΄μ κ° νμ€νμ§ μμ΅λλ€. μμ μ μνμ λ€λ©΄ 곧 μΆμ λ μμ μ΄λ μ μ κΈ°λ€λ € μ£ΌμκΈ° λ°λλλ€.
κ·Έλ¦¬κ³ μ΄κ²μ μ΄λ»μ΅λκΉ?
루νμμ νν¬λ₯Ό νΈμΆνλ κ²μ μΌλ°μ μΌλ‘ νμ©λμ§ μμ΅λλ€ ( forEach
). μ΄λ° μμΌλ‘ λ¬Έμ λ₯Ό μΌμΌν€λ κ²μ μ½μ΅λλ€.
useStoreProviders
useProvider
λ° useShouldComponentUpdate
λ λ€ Hooksλ‘ λ¬Έμ κ° μμ΅λλ€ (μ΄κ²μ΄ Reactμ νν¬κ°μλ μ΄μ μ
λλ€). https://github.com/facebook/react/issues/14534#issuecomment -455411307μμ λ΄ μλ΅μ
μ λ°μ μΌλ‘μ΄ λ¬Έμ μ μλλ₯Ό μ΄ν΄νλ λ° μ΄λ €μμ κ²ͺκ³ μμ΅λλ€.
λ€μ€ 컨ν
μ€νΈ μλΉλ useContext
νν¬λ‘ ν΄κ²°λ©λλ€. λ무 λ§μ 컨ν
μ€νΈλ₯Ό ꡬλ
νκ³ λ무 μμ£Ό λ€μ λ λλ§νλ κ΅¬μ± μμλ₯Ό μμ±νκΈ°κ° λ무 μ½κΈ° λλ¬Έμ λ°°μ΄λ‘ "μλν"νλ κ²μ κΆμ₯ useContext
APIκ° μ 곡νλ κ΅¬μ± μμκ° μμ νλ 컨ν
μ€νΈλ₯Ό λͺ
νν μ΄ν΄ν΄μΌν©λλ€. νμν κ²½μ° νΉμ 컨ν
μ€νΈλ₯Ό λͺ
μ μ μΌλ‘ μ¬μ©νλ useMyContexts()
νν¬λ₯Ό μμ±ν μ μμ΅λλ€. λ°°μ΄ κΈΈμ΄κ° λ³κ²½λλ©΄ κΉ¨μ§ μ μμΌλ―λ‘ λμ μΌλ‘ λ§λλ κ²μ κΆμ₯νμ§ μμ΅λλ€.
μ¬λ¬ 곡κΈμλ₯Ό λ°°μΉνλ κ²μ "보μΌλ¬ νλ μ΄νΈ"λ‘ λ³Ό μ μμΌλ©° κ²°κ΅ μ΄μ λν ν΄κ²°μ±
μ΄μμ μ μμ΅λλ€. κ·Έλ¬λ λλ λν λΉμ μ΄ κ·Έκ²μ ν° λ¬Έμ λ‘ λ³΄λ μ΄μ λ₯Ό μ΄ν΄νμ§ λͺ»ν©λλ€. μ΄ μ€λ λμ μλ λ¬Έμ λ₯Ό μ€λͺ
νκΈ°μ μΆ©λΆν νμ€μ μ΄μ§ μμ΅λλ€. μ ν리μΌμ΄μ
μλ¨ μ΄λκ°μ JSXμ μ¬λ¬ λ μ΄μ΄λ₯Ό μ€μ²©ν΄λ λμ κ²μ μμ΅λλ€. λλΆλΆμ κ΅¬μ± μμμ ν¨μ¬ λ κΉμ div
μ€μ²©μ΄ μμΌλ©° κ·Έλ€μ§ μμλμ§ μμ΅λλ€.
μ΄ μ μ λν΄ μ΄λ―Έ λ΅λ³ ν κ² κ°μμ λ§λ¬΄λ¦¬νκ² μ΅λλ€. ν λ‘ μ μμΌλ‘ μ§νλ©λλ€. λΉ μ§ κ²μ΄ μμΌλ©΄ μλ €μ£ΌμΈμ.
OT : @gaearon , useRender
κ°μ κ²μ μΆκ°νκ±°λ λ λλ§μ λ λ§μ΄ μ μ΄ ν κ³νμ΄ μμ΅λκΉ? μ :
useRender(() => <div />, [...props])
λ λ²μ§Έ μΈμλ useEffect
νν¬μ λμΌν μν μν©λλ€.
useMemo
λ λΉμ μ μΉκ΅¬μ
λλ€.
https://reactjs.org/docs/hooks-faq.html#how -to-memoize-calculationsμμ λ λ²μ§Έ μ€ λν«μ μ°Έμ‘°νμμμ€.
λλ λ€μκ³Ό κ°μ μ½λλ‘ λλ¬μ΅λλ€.
function provider<T>(theProvider: React.Provider<T>, value: T) {
return {
provider: theProvider,
value
};
}
function MultiProvider(props: {providers: Array<{provider: any; value: any}>; children: React.ReactElement}) {
let previous = props.children;
for (let i = props.providers.length - 1; i >= 0; i--) {
previous = React.createElement(props.providers[i].provider, {value: props.providers[i].value}, previous);
}
return previous;
}
κ·Έλ° λ€μ λ΄ μ΅μμ μ 곡 κ΅¬μ± μμμμ :
public render() {
return (
<MultiProvider
providers={[
provider(Context1.Provider, this.context1),
provider(Context2.Provider, this.context2),
provider(Context3.Provider, this.context3),
provider(Context4.Provider, this.context4),
provider(Context5.Provider, this.context5),
]}
><AppComponents />
</MultiProvider>
}
@gaearon
μ ν리μΌμ΄μ μλ¨ μ΄λκ°μ JSXμ μ¬λ¬ λ μ΄μ΄λ₯Ό μ€μ²©ν΄λ λμ κ²μ μμ΅λλ€.
λλ κ·Έλ° μμΌλ‘ μ£Όμ νκΈ°λ₯Ό μνλ ~ 15 μ’ μμ±μ κ°μ§κ³ μμΌλ©° 15 λ 벨μ λ€μ¬ μ°κΈ°λ λμκ² μμμ§ μμ΅λλ€. :)
@ 0xorial λΉμ μ
const compose = (contexts, children) =>
contexts.reduce((acc, [Context, value]) => {
return <Context.Provider value={value}>{acc}</Context.Provider>;
}, children);
λ€μκ³Ό κ°μ΄ μ¬μ©νμμμ€.
import Context1 from './context1';
import Context2 from './context2';
import Context3 from './context3';
...
import Context15 from './context15';
const MyComponent = (props) => {
// const value1..15 = ... get the values from somewhere ;
return compose(
[
[Context1, value1],
[Context2, value2],
[Context3, value3],
...
[Context15, value15],
],
<SomeSubComponent/>
);
}
μ΄ κ²½μ°λ₯Ό μ²λ¦¬νλ λΌμ΄λΈλ¬λ¦¬λ₯Ό κ³Όκ±°μ μμ±νμ΅λλ€ : https://github.com/disjukr/join-react-context
μ΄κ²μ μ ν리μΌμ΄μ
μμ νμ μΌμ΄λλ μΌμ
λλ€. useContext
μ κ΅¬μ± μμμ 컨ν
μ€νΈ λ°μ΄ν° _ μλΉ _μ μ ν©νμ§λ§ μ¬λ¬ 곡κΈμκ°μλ μ±μμ 컨ν
μ€νΈλ₯Ό _ μ 곡 _ν΄μΌνλ κ²½μ°μλ 그리 μ’μ§ μμ΅λλ€.
@alesmenzelsocialbakers μ루μ μ νμ λμμ λ€μκ³Ό κ°μ΅λλ€.
const composeProviders = (...Providers) => (Child) => (props) => (
Providers.reduce((acc, Provider) => (
<Provider>
{acc}
</Provider>
), <Child {...props} />)
)
const WrappedApp = composeProviders(
ProgressProvider,
IntentsProvider,
EntitiesProvider,
MessagesProvider
)(App)
ReactDOM.render(<WrappedApp />, document.getElementById('root'));
λ¨μ μ κ°κ°μ νΉμ Provider κ΅¬μ± μμλ₯Ό μμ±ν΄μΌνλ€λ κ²μ
λλ€.
μ:
export const ProgressProvider = ({ children }) => {
const [progress, setProgress] = useState(0)
return (
<ProgressContext.Provider value={{ progress, setProgress }}>
{children}
</ProgressContext.Provider>
)
}
μλΉμ€ ꡬμ±μ΄ λ λμ μν κ΄λ¦¬ λΌμ΄λΈλ¬λ¦¬ λ₯Ό λ§λ€μμ΅λλ€. λ€μμ 곡κΈμ μ§μ₯μ νΌνλ λ°λͺ¨μ λλ€ . μμ λ‘κ² μλνκ±°λ μμ€ (100 μ€μ μ½λ)λ₯Ό μ½μ΄λ³΄μΈμ!
컨ν μ€νΈ 곡κΈμλ₯Ό μμ§νλ "λ²μ"κ°μ²΄λ₯Ό λμ νμ¬ λ€μμ μνν©λλ€.
λλ μ΄κ²μ κ· μ΄μ΄ μκ²Όλ€. μ΄κ²μ μ μλνλ κ² κ°μ΅λλ€.
const composeWrappers = (
wrappers: React.FunctionComponent[]
): React.FunctionComponent => {
return wrappers.reduce((Acc, Current): React.FunctionComponent => {
return props => <Current><Acc {...props} /></Current>
});
}
μ¬μ©λ²μ λ€μκ³Ό κ°μ΅λλ€.
const SuperProvider = composeWrappers([
props => <IntlProvider locale={locale} messages={messages} children={props.children} />,
props => <ApolloProvider client={client}>{props.children}</ApolloProvider>,
props => <FooContext.Provider value={foo}>{props.children}</FooContext.Provider>,
props => <BarContext.Provider value={bar}>{props.children}</BarContext.Provider>,
props => <BazContext.Provider value={baz}>{props.children}</BazContext.Provider>,
]);
return (
<SuperProvider>
<MainComponent />
</SuperProvider>
);
μ΄ λμ°λ―Έλ₯Ό npm λΌμ΄λΈλ¬λ¦¬ react-compose-wrappers
λ‘λ κ²μνμ΅λλ€.
λ€μμ μΈμ¦ λ μ¬μ©μλ₯Ό νμν κ΅¬μ± μμλ‘ μ λ¬νλ λ°©λ²μ 보μ¬μ€λλ€.
λ΄ μμ© νλ‘κ·Έλ¨μ λν΄ νλμ μνλ₯Ό λ§λ€κΈ°λ‘ κ²°μ νμ΅λλ€. State.js νμΌμμ μ΄κΈ° μν, 컨ν μ€νΈ, κ°μκΈ°, 곡κΈμ λ° νν¬λ₯Ό μ€μ νμ΅λλ€.
import React, { createContext, useContext, useReducer } from 'react';
const INITIAL_STATE = {}
const Context = createContext();
const reducer = (state, action) =>
action
? ({ ...state, [action.type]: action[action.type] })
: state;
export const Provider = ({ children }) => (
<Context.Provider value={ useReducer(reducer, INITIAL_STATE) }>
{ children }
</Context.Provider>
);
const State = () => useContext(Context);
export default State;
κ·Έλ° λ€μ index.js νμΌμμ 곡κΈμμ λ΄ μ±μ λννμ΅λλ€.
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from './State';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<Provider>
<App />
</Provider>
</React.StrictMode>,
document.getElementById('root'),
);
κ΅¬μ± μμμμ μνλ₯Ό μ¬μ©νκΈ° μν΄ νν¬λ₯Ό μ¬μ©ν μ μμ΅λλ€. λμ€ν¨μΉλ₯Ό ββμ¬μ©νμ¬ μνλ₯Ό μ λ°μ΄νΈ ν μλ μμ΅λλ€. μλ₯Ό λ€μ΄ μ¬μ©μλ₯Ό μ»κ±°λ μ€μ νλ €λ κ²½μ°.
import React, {useEffect} from 'react';
import State from './State'
const ExampleComponent = () => {
const [{ user }, dispatch] = State();
useEffect(() => {
const getUser = async () => {
const data = await fetch('http://example.com/user.json'); // However you get your data
dispatch({ type: 'user', user: data });
}
getUser();
}, [dispatch]);
// Don't render anything until user is retrieved
// The user is undefined since I passed an empty object as my initial state
if(user === undefined) return null;
return(
<p>{user.name}</p>
);
}
export default ExampleComponent;
μ΄λ° μμΌλ‘ λ§μ μΆκ° 컨ν μ€νΈλ₯Ό μΆκ°νμ§ μκ³ νμν λ°©μμΌλ‘ μνλ₯Ό κ΅¬μΆ ν μμλ μμ λ₯Ό μ 곡νκ³ κ³΅κΈμμ κΉμ λ₯μ§λ₯Ό νΌνλ λ° λμμ΄λλ€κ³ μκ°ν©λλ€.
곧 μΆμ λ Hooks APIλ 컨ν μ€νΈλ₯Ό μλΉνλ λ€λ₯Έ λ°©λ²μ μ 곡ν©λλ€.
ν΄λμ€ κ΅¬μ± μμμμ μ΄λ»κ² μ¬μ©ν©λκΉ?
곧 μΆμ λ Hooks APIλ 컨ν μ€νΈλ₯Ό μλΉνλ λ€λ₯Έ λ°©λ²μ μ 곡ν©λλ€.
https://reactjs.org/docs/hooks-reference.html#usecontextν΄λμ€ κ΅¬μ± μμμμ μ΄λ»κ² μ¬μ©ν©λκΉ?
ν΄λμ€λ₯Ό μμ±νμ§ μκ³ λ€μν React κΈ°λ₯μ νμ©νλ λ° νν¬κ° μ¬μ©λμ§ μμ΅λκΉ?
μ¦, λ€μν νν¬κ° μννλ λͺ¨λ κ²μ΄ μ΄λ―Έ ν΄λμ€μ μ‘΄μ¬ν©λλ€. νΈλ¦¬ν ꡬ문 λ° μ¬μ© APIμ λν΄ μ΄μΌκΈ°νλ κ²½μ° λ°μμ΄ ν΄λμ€μμ κΈ°λ₯ κ΅¬μ± μμλ‘ μ΄λνλ―λ‘ ν¨μ λ° νν¬μ μ€μ κ²μ νμν©λλ€)
vue3μ μ μ¬ν APIλ₯Ό μ 곡νμ¬ λ¬Έμ λ₯Ό ν΄κ²°νλ ν¨ν€μ§λ₯Ό λ§λ€μμ΅λλ€.
https://github.com/TotooriaHyperion/react-multi-provide
μ£Όμ:
Outer.tsx
import React, { useMemo } from "react";
import { Providers, useCreateContexts, useProvide } from "../..";
import { createService, ServiceA } from "./service";
export const Outer: React.FC = ({ children }) => {
const contexts = useCreateContexts();
const service = useMemo(createService, []);
useProvide(contexts, ServiceA.id, service);
return <Providers contexts={contexts}>{children}</Providers>;
};
Inner2.tsx
import React from "react";
import { useContexts, useReplaySubject } from "../..";
import { ServiceA } from "./service";
export const Inner2: React.FC = () => {
const [
{
state$,
actions: { inc, dec },
},
] = useContexts([ServiceA.id]);
const count = useReplaySubject(state$);
return (
<>
<p>{count}</p>
<div>
<button onClick={inc}>Increment</button>
<button onClick={dec}>Decrement</button>
</div>
</>
);
};
λ°©λ²μ λ€μκ³Ό κ°μ΅λλ€.
interface Composable {
(node: React.ReactNode): React.ReactElement
}
const composable1: Composable = (node)=>{
return <someContext.Provider>{node}</someContext.Provider>
}
function Comp({children}:{children?:React.ReactNode}){
return pipe(
composabl1, composabl2, composable3
)(children)
}
rxjsμ κ°μ λ§μ μΈκΈ° λΌμ΄λΈλ¬λ¦¬μμ pipe
ν¨μλ₯Ό μ°Ύμ μ μμΌλ©°μ΄ νμ΄ν λΌμΈκ³Ό κ°μ μμ
μ λν μ¬λ¬ μΈμ΄ μμ€ μ μλ μμ΅λλ€. λ€λ₯Έ libλ₯Ό μ¬μ©νμ¬ 'ν΄κ²°'ν νμκ° μμ΅λλ€.
κ°μ₯ μ μ©ν λκΈ
@ 0xorial λΉμ μ
λ€μκ³Ό κ°μ΄ μ¬μ©νμμμ€.