首页 > 解决方案 > 如何在 react-redux-firebase 中编写 firestore 批处理

问题描述

在 react-redux-firebase 中,如何进行类似于 firestore 文档中显示的批量写入?(见下文)

// Get a new write batch
var batch = db.batch();

// Set the value of 'NYC'
var nycRef = db.collection("cities").doc("NYC");
batch.set(nycRef, {name: "New York City"});

// Update the population of 'SF'
var sfRef = db.collection("cities").doc("SF");
batch.update(sfRef, {"population": 1000000});

// Delete the city 'LA'
var laRef = db.collection("cities").doc("LA");
batch.delete(laRef);

// Commit the batch
batch.commit().then(function () {
    // ...
});

标签: react-redux-firebase

解决方案


像这样的东西会起作用。不要忘记这一点,react-redux-firebase并分别扩展和redux-firestore的原始实现。firebasefirestore

const Counter = () => {
  const firestore = useFirestore()
  const batch = firestore.batch()
  const nycRef = firestore.get({collection: 'cities', doc: 'NYC'})
  batch.set(nycRef, {name: 'New York City'})
  const sfRef = firestore.get({collection: 'cities', doc: 'SF'})
  batch.update(sfRef, {population: 10000000})
  const laRef = firestore.get({collection: 'cities', doc: 'LA'})
  firestore.delete(laRef)
  const runBatch = async () => await batch.commit()

  return <button onClick={runBatch}>Attempt Batch</button>
}

推荐阅读