我正在改变减速机的状态。在调试时,我检查了状态是否真的改变了。但是组件没有更新。
成分:
function Cliente(props) {
const dispatch = useDispatch()
const cliente = useSelector(({ erpCliente }) => erpCliente.cliente)
const { form, handleChange, setForm } = useForm(null)
...
function searchCepChangeFields() {
//This call the action and change the store on reducer
dispatch(Actions.searchCep(form.Cep))
.then(() => {
// This function is only taking values ??from the old state.
// The useSelector hook is not updating with store
setForm(form => _.setIn({...form}, 'Endereco', cliente.data.Endereco))
setForm(form => _.setIn({...form}, 'Uf', cliente.data.Uf))
setForm(form => _.setIn({...form}, 'Cidade', cliente.data.Cidade))
setForm(form => …Run Code Online (Sandbox Code Playgroud) 我想对 typeDef 中的代码进行语法高亮显示。有可能的?
有一个扩展吗?或者我必须以其他方式编码 typeDef ?
export const typeDef = `
type User {
_id: ID!
email: String!
password: String
createdEvents: [Event!]
}
type AuthData {
userId: ID!
token: String!
tokenExpiration: Int!
}
input UserInput {
email: String!
password: String!
}
`;
Run Code Online (Sandbox Code Playgroud) 我应该在每个子组件中使用相同的 useSelector 还是应该通过 props 传递 useSelector?
下面的两个例子都有效,我只想知道什么是最佳实践。
通过 props 传递的示例:
const FatherComponent = () => {
const userId = useSelector((state) => state.user.id);
return (
<ChildComponent userId={userId} />
)
}
const ChildComponent = ({userId}) => {
return (
<>{userId}</>
)
}
Run Code Online (Sandbox Code Playgroud)
或重复 useSelector 的示例:
const FatherComponent = () => {
const userId = useSelector((state) => state.user.id);
return (
<ChildComponent />
)
}
const ChildComponent = () => {
const userId = useSelector((state) => state.user.id);
return (
<>{userId}</>
)
}
Run Code Online (Sandbox Code Playgroud) 我在 ReactJS 商店中有以下产品列表:
{productsList.map((product) => (
<ProductItem
product={product}
/>
)}
Run Code Online (Sandbox Code Playgroud)
当我在此列表中添加更多产品时,滚动位置将转到列表的末尾。添加更多产品的代码:
function nextPage() {
getMoreProducts().then((newProducts)=> {
setProductsList(productsList.concat(newProducts))
})
}
Run Code Online (Sandbox Code Playgroud)
我需要保持滚动位置,因为这样用户必须滚动到顶部才能看到加载的新产品。
我尝试存储滚动位置,但更改是在列表增长之前完成的,所以什么也没有发生:
function nextPage() {
const actualScrollPosition = window.pageYOffset
getMoreProducts().then((newProducts)=> {
setProductsList(productsList.concat(newProducts)) //<- setState from React is async.
window.scroll({ top: actualScrollPosition, behavior: "smooth" });
})
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Prisma 创建自动增量 ID,但以 mongodb 作为数据源时不存在 autoincrement() 函数。
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
sequence Int @unique @default(autoincrement()) // This doesn't work with mongodb
email String @unique
password String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以进行查询来检查最后一个序列,但这看起来成本太高。
有更好的方法吗?
就像在 prisma.user.create() 中一样,有一种方法可以获取最后一个序列字段(就像我们对猫鼬所做的那样)?
ecmascript-6 ×3
javascript ×3
reactjs ×3
react-hooks ×2
react-redux ×2
graphql ×1
mongodb ×1
prisma ×1
use-state ×1