T extends Promise<any>[] | []
import { DependencyList, useEffect, useRef } from "react";
type Import = Promise<any>;
type AwaitedImports<T extends Import[]> = { [P in keyof T]: Awaited<T[P]> };
type Resolve<T extends Import[]> = (result: AwaitedImports<T>) => void;
function useEffectWithImports<T extends Import[] | []>(
resolve: Resolve<T>,
imports: () => T,
deps?: DependencyList
) {
const resolveRef = useRef(resolve);
const importsRef = useRef(imports);
resolveRef.current = resolve;
importsRef.current = imports;
useEffect(() => {
const promises = importsRef.current();
Promise.all(promises)
.then(resolveRef.current as Resolve<T>)
.catch(console.error);
}, deps);
}
useEffectWithImports(
([fs, path]) => {},
() => [import("fs"), import("path")],
[]
);
Мне этого не нужно было делать, потому что я использовал create-react-app, в конфиге вебпака которого уже произведена настройка sass?
Если п1 верный, то как в таком случае это реализовано? Я ведь мог установить не sass, а node-sass или dart-sass или любую другую библиотеку?
const fs = require("fs");
function modulesHasOneOf(moduleNames) {
return moduleNames.some(name => {
const modulePath = require.resolve(name);
return fs.existsSync(modulePath);
});
}
const isSassInstalled = modulesHasOneOf(["sass", "dart-sass", "node-sass"]);
const webpackConfig = {
...
module: {
rules: [
{
test: isSassInstalled ? /\.scss$/ : /\.css$/,
use: ["css-loader", isSassInstalled && "sass-loader"].filter(Boolean)
}
]
}
};
const config = {
module: {
rules: [
{
test: /\.css$/,
use: ["css-loader"]
},
{
test: /\.s(a|c)ss$/,
use: ["css-loader", "sass-loader"]
}
]
}
};
const Context = createContext();
function ContextProvider({ children }) {
const [active, setActive] = useState(false);
return <Context.Provider value={{ active, setActive }} >{children}</Context.Provder>
}
function App() {
return (
<ContextProvider>
<MyComponent />
</ContextProvider>
)
}
function MyComponent() {
const { active, setActive } = useContext(Context);
...
}
<div class="parent">
<div class="block-1"></div>
<div class="block-2"></div>
</div>
.parent {
display: flex;
flex-direction: column;
}
.block-2 {
flex-grow: 1;
}