react-native有std::invalid_argument(folly::to<std::string>("Expected ",method.callbacks," callbacks, but only ",count," parameters provided"))报错是怎么回事
问题原因
react-native出现std::invalid_argument(folly::to解决方案
在React Native中出现 std::invalid_argument(folly::to具体例子
在React Native中出现 std::invalid_argument 错误通常是由于回调函数的数量与传入参数的数量不匹配导致的。为了正确使用React Native并避免这个错误,需要确保在调用函数时传入正确数量的回调函数。
以下是一个具体的例子来说明如何正确使用React Native,并避免 std::invalid_argument 错误:
#include 
#include 
using namespace facebook;
struct SampleTurboModuleSpec : TurboModule {
  SampleTurboModuleSpec() = delete;
  SampleTurboModuleSpec(const std::string &methodName)
      : methodName(methodName) {}
  std::string getMethodName() { return methodName; }
  virtual jsi::Value invokeAndReturnJSValue(
      TurboModuleMethodValueProvider &valueProvider,
      const jsi::String &methodName,
      const jsi::Array &args) override {
    // Your implementation here
  }
 private:
  std::string methodName;
};
std::shared_ptr SampleSpec() {
  std::string methodName = "sampleMethod";
  return std::make_shared(methodName);
}
int main() {
  // Create a TurboModule
  auto sampleModule = SampleSpec();
  // Call the method with incorrect number of callbacks
  jsi::String methodName = jsi::String::createFromUtf8("sampleMethod");
  jsi::Array args(Runtime::get(), 0);
  // This call will trigger std::invalid_argument error
  sampleModule->invokeAndReturnJSValue({}, methodName, args);
  return 0;
}
    在以上例子中,模拟了一个简单的React Native TurboModule,并创建了一个名为 sampleMethod 的方法。在 main 函数中,我们通过传入空的回调函数数组来调用 sampleMethod 方法,这时就会触发 std::invalid_argument 错误。
为了避免这个错误,需要确保在调用 invokeAndReturnJSValue 方法时传入正确数量的回调函数。检查所调用的方法需要的回调函数数量,并传入相应数量的回调函数即可避免这个错误。
