Upgrade to Pro — share decks privately, control downloads, hide ads and more …

REACT SP - Ramda + React

REACT SP - Ramda + React

Ana Luiza Portello

July 13, 2018
Tweet

More Decks by Ana Luiza Portello

Other Decks in Programming

Transcript

  1. • faz uso de conceitos funcionais • 100% imutável •

    ganho em performance • elegante • point-free • etc
  2. • Lists(map, filter, reduce, contains, replace, passAll, crop, flatten, find)

    • Maths(inc, add, mean, sum) • String(split, replace) • Logics(equals, cond, not) • Relation(intersection, clamp, gt, lt) • Functions(curry, pipe, compose, ifElse, etc)
  3. const add = (x, y) => x + y add(1,

    2); // 3 add(1) // Error
  4. const curryAdd = R.curry((x, y) => x + y)) curryAdd(1,

    2); // 3 const addOne = curryAdd(1); // Function addOne(2) // 3
  5. const Container = children => (<div>{children}</div>); const List = children

    => (<ul>{children}</ul>); const ListItem = ({ id, text }) => (<li key={id}> <span>{text}</span> </li>);
  6. const TodoItem = { id: 123, text: 'Comprar pão' };

    Container(List(ListItem(TodoItem))); Comprar pão
  7. const TodoItems = [{...}, {...}, {...}] const TodoList = R.compose(

    Container, List, R.map(ListItems) ); TodoList(TodoItems); Comprar pão Pagar boletos Ir pra react sp na neon
  8. const TodoItems = [{...}, {...}, {...}] const TodoList = R.compose(

    Container, List, R.map(ListItems), R.sort(byId), ); const byId = R.ascend(R.prop(‘id’)) TodoList(TodoItems);
  9. const TodoItems = [{...}, {...}, {...}] const TodoList = R.compose(

    Container, List, sortedListItems, ); TodoList(TodoItems);
  10. // state { // …, // selected: false, // …

    , // } onToggleSelected() { this.setState((prevState) => ({ ...prevState selected: !prevState.selected })) }
  11. const createObj = R.applySpec({ counter: R.inc, userData: { phone: R.trim},

    }) createObj(0, “ 98499-1900”) // {1, userData{ phone: “9849919000”}}
  12. // state = { a: { b: { counter: 0

    } const counterLens = R.lensPath(['a','b','counter']); counterLens(state)
  13. // const state = {counter : 0} // view const

    getCounter = R.view(counterLens) // 0 getCounter(state) // 0 // set const setCounter = R.set(counterLens, 1) setCounter(state) // 1 // over const incCounter = R.over(counterLens, R.inc) incCounter(state) // 1
  14. • Se foca em apenas uma propriedade do objeto •

    Não muta a propriedade • Consigo lidar caso a propriedade é undefined • Torna bem mais declarativo
  15. switch (action.type) { case 'INCREMENT': return state + action.payload; case

    'DECREMENT': return state - action.payload; default: return state; }
  16. const Content = (props) => { if(props.loading) { return <Loading

    /> } if(props.items.length == 0) { return <Missing /> } return <Section {...props} /> }
  17. const Name = () => styled.Text` font-weight: bold, font-size: 18px,

    color: ${props => props.theme.primaryColor} `
  18. const Name = () => styled.Text` font-weight: bold, font-size: 18px,

    color: ${R.path([‘theme’, ‘primaryColor’])} `
  19. 5 + 10 // 15 R.add(5, 10) // 15 [5,

    10].reduce((x, y) => {x + y}, 0) [5, 10].reduce(add, 0)
  20. const obj = {x: 10} R.prop(‘x’)(obj) // 10 obj.x //

    10 R.sortBy(R.prop(‘x’)) R.prop(‘x’)({}) // undefined
  21. • Rambda - github.com/selfrefactor/rambda • Ramda-fantasy - github.com/ramda/ramda-fantasy • Ramda

    React Redux Patterns - tommmyy.github.io/ramda-react-redux-patterns/ • Thinking Ramda - randycoulman.com/blog/2016/05/24/thinking-in-ramda-getting -started • Ramda - Derek Stavis(Pagar.me talks) • Hey Underscore, You’re doing it wrong - Brian Lonsdorf.