小编Dan*_*nha的帖子

当Reducer 中的store 发生变化时,useSelector 不会更新。ReactJS Redux

我正在改变减速机的状态。在调试时,我检查了状态是否真的改变了。但是组件没有更新。

成分:

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)

javascript ecmascript-6 reactjs react-redux react-hooks

40
推荐指数
2
解决办法
4万
查看次数

有一种方法可以在 vscode 中的` `(重音符号)内语法高亮 GraphQl TypeDef 吗?

我想对 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)

graphql visual-studio-code

6
推荐指数
2
解决办法
3143
查看次数

使用 Hooks 和 Redux 的 ReactJS 良好实践。我应该在每个组件中使用 useSelector,还是应该通过 props 传递?

我应该在每个子组件中使用相同的 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)

javascript ecmascript-6 reactjs react-redux

6
推荐指数
1
解决办法
1437
查看次数

在 ReactJS 上的列表中连接更多项目时保持滚动位置

我在 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)

javascript ecmascript-6 reactjs react-hooks use-state

6
推荐指数
1
解决办法
3770
查看次数

使用mongodb在Prisma上创建自动增量ID的正确方法

我正在尝试使用 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() 中一样,有一种方法可以获取最后一个序列字段(就像我们对猫鼬所做的那样)?

mongodb prisma

0
推荐指数
1
解决办法
3858
查看次数