这是我第一次使用 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/