小编Ele*_*ark的帖子

如何使用 useQuery 钩子填充其他钩子中的状态?

我最近一直在处理一些与钩子相关的问题,因为我一直在我的一个项目中实施钩子。我不断收到错误“渲染的钩子比上一次渲染时多”。

似乎让我的代码工作的唯一方法是将 useQuery 钩子放在所有其他钩子之后。然而,这是一个问题,因为我想用查询数据中的值填充一些状态值。

// code that doesn't error, but am not able to initialize state with query values

const [url, setUrl] = useState('')

const updateLink = useMutation(LINK_UPDATE_MUTATION, {
  variables: {
    id: props.id,
    url
  }
})

const { data, loading, error } = useQuery(LINK_QUERY, {
  variables: {
    id: props.id
  }
})

if (loading) {
  return <div>Loading...</div>
}
if (error) {
  return <div>Error! {error.message}</div>
}
Run Code Online (Sandbox Code Playgroud)

对比

// code that errors with 'Rendered more hooks than during the previous render.'

const { …
Run Code Online (Sandbox Code Playgroud)

reactjs react-apollo react-hooks

9
推荐指数
1
解决办法
1万
查看次数

尝试从新的 Google Cloud API Gateway 获取时出现 CORS 错误

我正在测试新的 API 网关,以保护我的 React 应用程序的云功能。到目前为止,这个过程比之前的替代方案要好得多,但是当我尝试从我的 React 应用程序访问我的 API 网关时,我目前遇到了 CORS 错误。我在我的 Cloud Function 中正确设置了 CORS 标头,但我不知道在 API Gateway 端点上执行相同操作的方法。我正在使用 Postman 测试对网关端点的请求,并且一切正常,所以这正是我从 React 应用程序请求时。

错误:“从源“https://example.netlify.app”获取“https://my-gateway-a12bcd345e67f89g0h.uc.gateway.dev/hello?key=example”的访问权限已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用了 CORS 的资源。”

希望对这个问题有所了解。谢谢!

cors reactjs google-cloud-platform google-cloud-functions google-cloud-api-gateway

7
推荐指数
3
解决办法
2777
查看次数

使用 Sharp 库运行 AWS Lambda 函数时出现问题

我按照 AWS 研讨会 ( https://amplify-workshop.go-aws.com/70_generate_thumbnails/10_creating_a_photo_processor_lambda.html ),通过 aws-amplify CLI 创建了一个 lambda 函数。似乎某个地方的 Node.js 版本存在冲突。

我认为 Sharp 库是问题所在,因此我尝试将版本更改为最新版本,看看是否会起到任何作用,但它没有解决问题。

云观察错误日志:

module initialization error: Error
was compiled against a different Node.js version using
NODE_MODULE_VERSION 67. This version of Node.js requires
NODE_MODULE_VERSION 57. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
at Object.Module._extensions..node (module.js:681:18)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/node_modules/sharp/lib/constructor.js:10:15)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10) …
Run Code Online (Sandbox Code Playgroud)

node.js aws-lambda sharp aws-amplify

3
推荐指数
1
解决办法
5381
查看次数