Last active
May 17, 2020 17:25
-
-
Save ikenfin/08557752f790ac9c947fc4bb36e111b4 to your computer and use it in GitHub Desktop.
Is it normal way to use React?:)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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