Skip to content

Instantly share code, notes, and snippets.

@ikenfin
Last active May 17, 2020 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikenfin/08557752f790ac9c947fc4bb36e111b4 to your computer and use it in GitHub Desktop.
Save ikenfin/08557752f790ac9c947fc4bb36e111b4 to your computer and use it in GitHub Desktop.
Is it normal way to use React?:)
import React from 'react'
import * as R from 'ramda'
// Group data by day
// [ {*}, ... ] -> { x: [*], ... }
const groupByDay = R.groupBy(item => {
const date = new Date(parseInt(item.date))
return date.getDate()
})
// render multiple
const renderGroupedByDay = R.mapObjIndexed((items, day) => {
return <div key={ day }>
<strong>{ day }</strong>
<ol>
{ items.map((item, index) => (
<li key={ index }>{ item.name }</li>
)) }
</ol>
</div>
})
// Multiple components composition
export default function TestComponent ({ items }) {
const components = R.compose(
Object.values,
renderGroupedByDay,
groupByDay
)(items)
return components
}
// Sample data
const items = [
{
name: 'A:1',
date: Date.now()
},
{
name: 'A:2',
date: Date.now()
},
{
name: 'B',
date: Date.now() - 104000000
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment