我正在尝试在我的React项目中使用firebase提供身份验证和数据库功能。
在我App.js我有
import app from "firebase/app";
import "firebase/auth";
app.initializeApp(firebaseConfig);
Run Code Online (Sandbox Code Playgroud)
在我称为<Component />渲染的其他组件中,App.js我可以使用它来初始化数据库
import app from "firebase/app";
import "firebase/firestore";
const db = app.firestore();
Run Code Online (Sandbox Code Playgroud)
但是这次我得到这个错误
Uncaught FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).
Run Code Online (Sandbox Code Playgroud)
因此,我也尝试加入app.initializeApp(firebaseConfig);此组件,但是又遇到了一个新错误,告诉我我实例化了两次。
Uncaught FirebaseError: Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app).
Run Code Online (Sandbox Code Playgroud)
因此,我想出的一种解决方法是在创建数据库App.js之后立即创建一个上下文,并将值传递给该上下文并使用。但是我不知道这是否是一个好的解决方案。app.initializeApp(firebaseConfig);const db = app.firestore();<Component />
我的问题与“出于某种原因如何检查Firebase应用程序是否已在Android上初始化”不同。我并没有尝试连接到第二个Firebase应用程序,因为它是针对该问题的。我整个项目只有一个Firebase应用程序,可以提供两种服务:auth和数据库。
我尝试了该问题的解决方案以用于 <Component />
if (!app.apps.length) {
app.initializeApp(firebaseConfig);
}
const …Run Code Online (Sandbox Code Playgroud) 我是网络编程的新手,我了解到我们可以输入npm i -D prettier来安装 prettier 作为其中之一,devDependency并使用它来格式化我们的代码。然后我发现有一个 VS Code 扩展 Pretty 可以做完全相同的事情。
我不知道这两者之间有什么区别。
如果我只安装扩展,我可以格式化没有安装漂亮 npm 包的代码库吗?
这两者的配置过程也不同吗?哪一个是首选?
我发现never当不可能的交叉点发生时,TypeScript 会在不同级别生成类型
\ntype Foo = {name: string, age: string}\ntype Bar = {name: string, age: number}\n\ntype Baz = Foo & Bar\nconst baz: Baz = {name: '123,', age:13} // \xe2\x9d\x8c Type 'number' is not assignable to type 'never'.\n\nRun Code Online (Sandbox Code Playgroud)\n上面的例子导致类型Baz为{name: string, age:never}
但是如果我们稍微改变一下Bar
type Foo = {name: string, age: string}\ntype Bar = {name: string, age: boolean}\n\ntype Baz = Foo & Bar // the whole `Baz` type is `never`\nRun Code Online (Sandbox Code Playgroud)\n现在整个Baz类型是never …
我正在尝试将 Gatsby starter 与 Netlify CMS 一起使用。https://github.com/stackrole-dev/gatsby-starter-foundation
\n\n我完全按照说明进行操作,但在启用 Git Gateway 后,当我尝试以管理员身份登录时,我遇到了此错误消息。
\n\nYour Git Gateway backend is not returning valid settings. Please make sure it is enabled.\nRun Code Online (Sandbox Code Playgroud)\n\n我不知道为什么它不起作用。
\n\n我的config.yml是
backend:\n name: git-gateway\n commit_messages:\n create: \'Create {{collection}} \xe2\x80\x9c{{slug}}\xe2\x80\x9d\'\n update: \'Update {{collection}} \xe2\x80\x9c{{slug}}\xe2\x80\x9d\'\n delete: \'Delete {{collection}} \xe2\x80\x9c{{slug}}\xe2\x80\x9d\'\n uploadMedia: \'[skip ci] Upload \xe2\x80\x9c{{path}}\xe2\x80\x9d\'\n deleteMedia: \'[skip ci] Delete \xe2\x80\x9c{{path}}\xe2\x80\x9d\'\n\nlocal_backend: true # run npx netlify-cms-proxy-server for local testing\n\nmedia_folder: "static/assets" \npublic_folder: "/assets" \n\ncollections:\nRun Code Online (Sandbox Code Playgroud)\n 我是 Redux 和 Redux 工具包的新手。我了解到createSelector可以接受多个输入选择器,它们可以作为单独的参数或数组提供。所有输入选择器的结果作为单独的参数提供给输出选择器。
const selectA = state => state.a;
const selectB = state => state.b;
const selectC = state => state.c;
const selectABC = createSelector(
[selectA, selectB, selectC],
(a, b, c) => {
// do something with a, b, and c, and return a result
return a + b + c;
}
);
Run Code Online (Sandbox Code Playgroud)
我的问题是,如果我只关心一个简单的状态,我可以这样使用useSelector吗
const selectA = state => state.a;
const a = useSelector(selectA)
Run Code Online (Sandbox Code Playgroud)
这两种用法有什么区别?
我有两个接口A,B它们有一个共同的属性
interface A {
a: boolean;
b: number;
c: string;
d: boolean;
}
interface B {
d: string;
}
interface C extends A,B {}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想C成为
{
a: boolean;
b: number;
c: string;
d: boolean | string; <- merged
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?我知道我可以Pick用来省略相同的密钥,然后扩展它,但还有其他方法吗?
不确定这是所谓的“发布/订阅”模式还是“发布/订阅”模式的一种形式。我正在尝试创建一个共享状态,以便不同的组件可以订阅它,并且只有在该状态有更新时才会更新。
const useForceUpdate = () => useReducer((state) => !state, false)[1];
const createSharedState = (reducer, initialState) => {
const subscribers = [];
let state = initialState;
const dispatch = (action) => {
state = reducer(state, action);
subscribers.forEach((callback) => callback());
};
const useSharedState = () => {
const forceUpdate = useForceUpdate();
useEffect(() => {
const callback = () => forceUpdate();
subscribers.push(callback);
const cleanup = () => {
const index = subscribers.indexOf(callback);
subscribers.splice(index, 1);
};
return cleanup;
}, []);
return [state, dispatch];
}; …Run Code Online (Sandbox Code Playgroud) 我了解到,对于基于会话的身份验证,会话 ID 通常存储在浏览器的 cookie 中,并且会在每次请求时发送回服务器。
我猜想发送会话 ID 的途径有多种(cookie、标头、请求正文、URL 等)那么,在 cookie、HTTP 标头、请求正文甚至 URL 中存储会话 ID 的含义或权衡是什么?
我有一个问题useRef:如果我添加ref.current到 的依赖项列表中useEffect,并且当我更改了 的值时ref.current,useEffect将不会触发内部的回调。
例如:
export default function App() {
const myRef = useRef(1);
useEffect(() => {
console.log("myRef current changed"); // this only gets triggered when the component mounts
}, [myRef.current]);
return (
<div className="App">
<button
onClick={() => {
myRef.current = myRef.current + 1;
console.log("myRef.current", myRef.current);
}}
>
change ref
</button>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
不应该是当useRef.current变化时,里面的东西useEffect就会运行吗?
另外,我知道我可以用useState在这里。这不是我要问的。而且我也知道ref在重新渲染期间保持参照不变,所以它不会改变。但我不是在做类似的事情
const myRef = useRef(1);
useEffect(() …Run Code Online (Sandbox Code Playgroud) 我有一个字符串数组要显示const array = ["one", "two", "three"]; 。
UI 最初显示数组中的第一项,即"one"。从那里我有一个按钮right,单击它会显示下一个项目或字符串,即two,然后three,之后three应该返回one并再次从那里开始。我还有一个left按钮,单击时它显示前一个项目或字符串,如果当前字符串是two,则前一个字符串是one,然后从one开始three并向后走。
我正在使用生成器来做到这一点。这是我的尝试
function* stepGen(steps) {
let index = 0;
while (true) {
const direction = yield steps[index];
index = (index + (direction === "forward" ? 1 : -1)) % steps.length;
}
}
const array = ["one", "two", "three"];
let gen = stepGen(array);
const getNext = () => gen.next("forward").value;
const …Run Code Online (Sandbox Code Playgroud) javascript ×6
reactjs ×6
typescript ×2
cookies ×1
ecmascript-6 ×1
firebase ×1
gatsby ×1
http ×1
netlify ×1
netlify-cms ×1
prettier ×1
redux ×1