LoginSignup
0
1

More than 3 years have passed since last update.

nodejsでtail

Posted at

electron(nodejs)上で特定のファイルをtailする

tailライブラリを使用する。
https://github.com/lucagrulla/node-tail

$ yarn add tail
App.tsx
import React, { useEffect, useState } from 'react'
import { render } from 'react-dom'
import { Tail } from 'tail'

const tail = new Tail('file-path')

const App = () => {
  let [texts, setTexts] = useState<string[]>([])

  useEffect(() => {
    tail.on('line', (data : string) => {
      texts = [...texts, data]
      setTexts(texts)
    })

    return () => {
      tail.unwatch()
    }
  }, [])

  return (
    <>
      {
        texts.map((text, index) => {
          return (
            <div key={index}>{text}</div>
          )
        })
      }
    </>
  )
}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1