State management library from Facebook
Will it kill redux?
const App = () => {
return (
<div>
<Counter />
</div>
)
}
const Counter = () => {
const [value, setValue] = useState(0)
return (
<>
<div>{value}</div>
<button onClick={() => setValue(prev => prev + 1)}>+</button>
</>
)
}
const App = () => {
return (
<div>
<Counter />
<ByTwo />
</div>
)
}
const Counter = () => {
const [value, setValue] = useState(0)
return (
<>
<div>{value}</div>
<button onClick={() => setValue(prev => prev + 1)}>+</button>
</>
)
}
const ByTwo = ({ value }) => <div>{value * 2}</div>
const App = () => {
const [value, setValue] = useState(0)
return (
<div>
<Counter value={value} setValue={setValue} />
<ByTwo value={value} />
</div>
)
}
const Counter = ({ value, setValue }) => {
return (
<>
<div>{value}</div>
<button onClick={() => setValue(prev => prev + 1)}>+</button>
</>
)
}
const ByTwo = ({ value }) => <div>{value * 2}</div>
const App = () => {
const [value, setValue] = useState(0)
return (
<div>
<Counter value={value} setValue={setValue} />
<ByTwo value={value} />
<VeryExpensiveSubTreeToReRender>???</VeryExpensiveSubTreeToReRender>
</div>
)
}
const Counter = ({ value, setValue }) => {
return (
<>
<div>{value}</div>
<button onClick={() => setValue(prev => prev + 1)}>+</button>
</>
)
}
const ByTwo = ({ value }) => <div>{value * 2}</div>
const Ctx = React.createContext()
const App = () => {
const [value, setValue] = useState(0)
return (
<Ctx.Provider value={{ value, setValue }}>
<div>
<VeryExpensiveSubTreeToReRender>
<Counter />
<ByTwo />
</VeryExpensiveSubTreeToReRender>
</div>
</Ctx.Provider>
)
}
const Counter = () => {
const { value, setValue } = useContext(Ctx)
return (
<>
<div>{value}</div>
<button onClick={() => setValue(prev => prev + 1)}>+</button>
</>
)
}
const ByTwo = () => {
const { value } = useContext(Ctx)
return <div>{value * 2}</div>
}
const Ctx = React.createContext()
const VeryExpensiveSubTreeToReRender = React.memo(ExpensiveSubTree);
const App = () => {
const [value, setValue] = useState(0)
return (
<Ctx.Provider value={{ value, setValue }}>
<div>
<VeryExpensiveSubTreeToReRender>
<Counter />
<ByTwo />
</VeryExpensiveSubTreeToReRender>
</div>
</Ctx.Provider>
)
}
const Counter = () => {
const { value, setValue } = useContext(Ctx)
return (
<>
<div>{value}</div>
<button onClick={() => setValue(prev => prev + 1)}>+</button>
</>
)
}
const ByTwo = () => {
const { value } = useContext(Ctx)
return <div>{value * 2}</div>
}
// const reducer = () => ...
const store = createStore({ reducer })
const App = () => {
return (
<Provider store={store}>
<div>
<Counter />
<ByTwo />
<VeryExpensiveSubTreeToReRender>???</VeryExpensiveSubTreeToReRender>
</div>
</Provider>
)
}
const Counter = () => {
const value = useSelector((state) => state.value);
const dispatch = useDispatch();
return (
<>
<div>{value}</div>
<button onClick={() => dispatch(setValue(value + 1))}>+</button>
</>
)
}
const ByTwo = () => {
const value = useSelector((state) => state.value);
return <div>{value * 2}</div>
}
const App = () => {
return (
<div>
<Counter />
<ByTwo />
</div>
)
}
const Counter = () => {
const [value, setValue] = useState(0)
return (
<>
<div>{value}</div>
<button onClick={() => setValue(prev => prev + 1)}>+</button>
</>
)
}
const ByTwo = ({ value }) => <div>{value * 2}</div>
import { RecoilRoot } from "recoil"
const App = () => {
return (
<RecoilRoot>
<div>
<Counter />
<ByTwo />
</div>
</RecoilRoot>
)
}
const Counter = () => {
const [value, setValue] = useState(0)
return (
<>
<div>{value}</div>
<button onClick={() => setValue(prev => prev + 1)}>+</button>
</>
)
}
const ByTwo = ({ value }) => <div>{value * 2}</div>
import { RecoilRoot, atom } from "recoil"
const counter = atom({
key: "counter",
default: 0
})
const App = () => {
return (
<RecoilRoot>
<div>
<Counter />
<ByTwo />
</div>
</RecoilRoot>
)
}
const Counter = () => {
const [value, setValue] = useState(0)
return (
<>
<div>{value}</div>
<button onClick={() => setValue(prev => prev + 1)}>+</button>
</>
)
}
const ByTwo = ({ value }) => <div>{value * 2}</div>
import { RecoilRoot, atom, useRecoilState } from "recoil"
const counter = atom({
key: "counter",
default: 0
})
const App = () => {
return (
<RecoilRoot>
<div>
<Counter />
<ByTwo />
</div>
</RecoilRoot>
)
}
const Counter = () => {
const [value, setValue] = useRecoilState(counter)
return (
<>
<div>{value}</div>
<button onClick={() => setValue(prev => prev + 1)}>+</button>
</>
)
}
const ByTwo = ({ value }) => <div>{value * 2}</div>
import { RecoilRoot, atom, useRecoilState, useRecoilValue } from "recoil"
const counter = atom({
key: "counter",
default: 0
})
const App = () => {
return (
<RecoilRoot>
<div>
<Counter />
<ByTwo />
</div>
</RecoilRoot>
)
}
const Counter = () => {
const [value, setValue] = useRecoilState(counter)
return (
<>
<div>{value}</div>
<button onClick={() => setValue(prev => prev + 1)}>+</button>
</>
)
}
const ByTwo = () => {
const value = useRecoilValue(counter);
return <div>{value * 2}</div>
}
import { RecoilRoot, atom, useRecoilState, useRecoilValue, selector } from "recoil"
const counter = atom({
key: "counter",
default: 0
})
const byTwo = selector({
key: "byTwo",
get: ({ get }) => {
const counterValue = get(counter);
return counterValue * 2
}
})
// App
// Counter
const ByTwo = () => {
const value = useRecoilValue(counter);
return <div>{value * 2}</div>
}
import { RecoilRoot, atom, useRecoilState, useRecoilValue, selector } from "recoil"
const counter = atom({
key: "counter",
default: 0
})
const byTwo = selector({
key: "byTwo",
get: ({ get }) => {
const counterValue = get(counter);
return counterValue * 2
}
})
// App
// Counter
const ByTwo = () => {
const value = useRecoilValue(byTwo);
return <div>{value}</div>
}
const currentUserNameQuery = selector({
key: 'CurrentUserName',
get: async ({get}) => {
const response = await myDBQuery({
userID: get(currentUserIDState),
});
return response.name;
},
});
function CurrentUserInfo() {
const userName = useRecoilValue(currentUserNameQuery);
return <div>{userName}</div>;
}
function MyApp() {
return (
<RecoilRoot>
<React.Suspense fallback={<div>Loading...</div>}>
<CurrentUserInfo />
</React.Suspense>
</RecoilRoot>
);
}