
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
@readr-media/react-election-widgets
Advanced tools
DataLoader class 可以撈取多種不同選舉的結果,和執行一次性資料撈取或是週期性資料撈取。
目前支援的選舉類型(type)包括:
councilMember:縣市議員mayor: 縣市長president: 總統referendum: 公投legislator: 立法委員(包含:區域/平地原住民/山地原住民/不分區政黨)年份(year)的部分,根據不同的選舉類型,有不同的選舉年份。
目前支援的年份包括:
然而,因為每種選舉的資料維度不盡相同,
所以在使用 DataLoader 時,需要提供「區域」(district)。
taipeiCitynewTaipeiCitytaoyuanCitytaichungCitytainanCitykaohsiungCityhsinchuCountymiaoliCountychanghuaCountynantouCountyyunlinCountychiayiCountypingtungCountyyilanCountyhualienCountytaitungCountypenghuCountykinmenCountylienchiangCountykeelungCityhsinchuCitychiayiCityall:範例:
import evc from '@readr-media/react-election-votes-comparison'
const DataLoader = evc.DataLoader
let ldr
// 抓取「2018 年台北市議員」選舉結果
ldr = new DataLoader({
type: 'councilMember',
year: '2018',
district: 'taipeiCity',
})
const data = await ldr.loadData()
// 抓取「2020 年總統」選舉結果
ldr = new DataLoader({
type: 'president',
year: '2020',
district: 'all',
})
const data = await ldr.loadData()
// 抓取「2020 年不分區立法委員」選舉結果
ldr = new DataLoader({
type: 'legislator',
subtype: 'party'
year: '2020',
district: 'all',
})
const data = await ldr.loadData()
// 抓取「2022 年公投」選舉結果
ldr = new DataLoader({
type: 'referendum',
year: '2022',
district: 'all',
})
const data = await ldr.loadData()
DataLoader 除了 loadData method 可以使用,亦可以使用 loadDataPeriodically method。
該 method 會解析 response header 中的 Cache-Control,將 max-age 的值抓出來,當作下一次 request 的 timer。
如果 max-age 不存在,則 timer 會設定為一個小時。
註:
目前沒有處理 no-store,如果有需要,需要再補邏輯。
範例:
// anotherComponent.jsx
import EVC from '@readr-media/react-election-votes-comparison'
const DataLoader = EVC.DataLoader
const EVCComponent = EVC.ReactComponent.EVC
function AnotherComponent(props) {
const [data, setData] = useState({})
useEffect(() => {
let dataLoader = new DataLoader({
year: '2018', // 年份
type: 'councilMember', // 選舉類型
district: 'taipeiCity', // 縣市
})
const handleError = (errMsg, errObj) => {
// do something for loading error
}
const handleData = (data) => {
// call React component `setState`
setState(data)
}
dataLoader.addEventListener('error', handleError)
dataLoader.addEventListener('data', setState)
// after register event listener
// start to load data periodically
dataLoader.loadDataPeriodically()
return () => {
dataLoader.removeEventListener('error', handleError)
dataLoader.removeEventListener('data', setState)
dataLoader = null
}
}, [])
return (
<EVCComponent
election={data}
device="rwd" // value could be 'rwd' or 'mobile'
/>
)
}
EVC component 的 UI 是參考下面 mockups 實作:
使用上僅需將 DataLoader 抓取回來的資料丟入 EVC 的 election prop 即可。
由於選舉模板的票數比較的 mockup,需要 EVC 可以強制 render mobile 版本,
所以在 EVC props 上,提供了 device prop 給使用者。
當 device="mobile" 時,EVC 會強制呈現 mobile 版本;當 device="rwd"(預設)時,
EVC 會根據 media query 來決定要 render 哪個版本。
Sample codes:
import evc from '@readr-media/react-election-votes-comparison'
const DataLoader = evc.DataLoader
const EVCComponent = evc.ReactComponent.EVC
async function load() {
const dataLoader = new DataLoader({
year: '2018', // 年份
type: 'councilMember', // 選舉類型
district: 'taipeiCity', // 縣市
})
let data
try {
// fetch data once
data = await dataLoader.loadData()
} catch(err) {
// handle error
}
return data
}
function renderComponent(data) {
return (
<EVCComponent
election={data}
device="rwd" // value could be 'rwd' or 'mobile'
/>
)
}
yarn install
$ yarn dev
// or
$ npm run dev
// or
$ make dev
$ npm run build
// or
$ make build
After executing Build scripts, we will have ./dist and /lib folders,
and then we can execute publish command,
npm publish
Note: before publish npm package, we need to bump the package version first.
FAQs
Unknown package
The npm package @readr-media/react-election-widgets receives a total of 2 weekly downloads. As such, @readr-media/react-election-widgets popularity was classified as not popular.
We found that @readr-media/react-election-widgets demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authorities’ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.