MUI 组件中的 style 和 sx prop 几乎做同样的事情。sx 属性提供了一些简写语法,并允许您访问主题对象。但除此之外,它们看起来完全相同。你什么时候应该使用其中一种而不是另一种?
我正在学习 Redux 工具包。从 React POV 来看,使用内联箭头函数从 useSelector 中访问您需要的状态的任何部分似乎非常直观,然后进行任何计算。作为示例,请考虑 redux 存储中的购物车商品及其数据(例如商品计数)。
function CartItemCounter({ itemId }){
const cart = useSelector(state => state.cart);
const itemInCart = cart.items[itemId];
const count = itemInCart?.count || 0;
return <div>{itemId} - {count} nos</div>
}
Run Code Online (Sandbox Code Playgroud)
但我看到所有这些信息都表明您应该在切片旁边定义选择器,使用 createSelector 等。什么是正确的方法,为什么更好?
firebase 文档在文档的一部分中向他们展示了在 getAuth() 调用中使用应用程序实例
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
const firebaseConfig = {
// ...
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app); //I'm talking about this line
Run Code Online (Sandbox Code Playgroud)
然后在同一页面的下一部分中,他们继续使用 getAuth() 而不使用应用程序实例,以便使用电子邮件登录用户。
import { getAuth, createUserWithEmailAndPassword } from "firebase/auth";
const auth = getAuth();
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user;
})
.catch((error) => {
console.log(error)
});
Run Code Online (Sandbox Code Playgroud)
正确的使用方法是什么?
我有一个受歧视的工会
type MyDUnion = { type: "anon"; name: string } | { type: "google"; idToken: string };
Run Code Online (Sandbox Code Playgroud)
我想MyDUnion直接从类型中访问判别联合中的名称键。像这样的东西
type Name = MyDUnion['name']
Run Code Online (Sandbox Code Playgroud)
但打字稿不允许这样做
Property 'name' doesn't exist on type '{ type: "anon"; name: string } | { type: "google"; idToken: string }'
Run Code Online (Sandbox Code Playgroud)
我怎样才能访问它?
需要明确的是,这不是一个有效的解决方案:
type MyName = string;
type MyDUnion = { type: "anon"; name: MyName } | { type: "google"; idToken: string };
type Name = MyName; // this line would be in a different …Run Code Online (Sandbox Code Playgroud)