reactjs - 如何在 React 中使用 API webhook 来接收通知

标签 reactjs webhooks github-webhook

这是我第一次使用 Webhooks,我想知道是否可以从 Webhooks 订阅接收通知。我正在为我的项目使用 create-react-app,并希望使用 github 的 Webhooks 订阅在每次提交到我的 github 存储库时发送警报。我将如何处理这个项目以及我需要做什么来测试我的 Webhooks 订阅?

最佳答案

我希望这个提示有帮助!不要忘记投票加强。让我们开始吧...

我建议使用网络套接字,您的应用程序将在其中监听后端。

这将阻止您为后端创建请求循环。

更多信息...

React only consumes information, who provides and controls is only the backend.



希望这可以帮助。成功!
class Main extends Component {
    ......
    // instance of websocket connection as a class property
    ws = new WebSocket('ws://localhost:3000/ws')
    componentDidMount() {
        this.ws.onopen = () => {
        // on connecting, do nothing but log it to the console
        console.log('connected')
        }
        this.ws.onmessage = evt => {
        // listen to data sent from the websocket server
        const message = JSON.parse(evt.data)
        this.setState({dataFromServer: message})
        console.log(message)
        }
        this.ws.onclose = () => {
        console.log('disconnected')
        // automatically try to reconnect on connection loss
        }
    }
    render(){
        <ChildComponent websocket={this.ws} />
    }
}

In the example I used a class component. A tip to take advantage of modern features would be to start with functional components.

关于reactjs - 如何在 React 中使用 API webhook 来接收通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61789285/

相关文章:

git - 使用 gitlab webhooks 在 Windows 服务器上部署

php - 使用 PHP 转换 WooCommerce Webhook 有效负载

node.js - 使用 GitHub Webhook 使用 dockerimage 转发到 Jenkins 时出现 Nodejs 错误 "EPROTO"

github - 使用 github webhook 阅读文档结果为 400 : "Payload not valid, invalid or missing signature"

javascript - Prop 验证 react / Prop 类型中缺少

javascript - 在 React 中单击组件时将类添加到 sibling

javascript - react 前端连接到 flask 后端 Howto

reactjs - 使用 useRef react createPortal 不起作用

jenkins - Gitlab webhook 获取错误:500 内部服务器错误 - URI::InvalidURIError