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. ANA LUIZA BASTOS
    github.com/anabastos
    @naluhh
    @anapbastos
    Software Developer na Quanto e
    cientista da computação na
    PUC-SP
    anabastos.me

    View Slide

  2. JSLADIES
    fb.com/jsladiesbr
    twitter.com/jsladiessp
    meetup.com/JsLadies-BR/
    LAMBDA.IO
    t.me/lambdastudygroup
    github.com/lambda-study-group/
    meetup.com/Lambda-I-O-Sampa-
    Meetup/

    View Slide

  3. INTRODUÇAO AO RAMDA COM
    REACT

    View Slide

  4. Biblioteca que foi pensada
    para tornar mais fácil o
    javascript funcional

    View Slide

  5. REACT + RAMDA

    View Slide

  6. USOS
    CONVENIÊNTES
    /
    REFACTORZINHOS

    View Slide

  7. PQ RAMDA?

    View Slide

  8. ● faz uso de conceitos funcionais
    ● 100% imutável
    ● ganho em performance
    ● elegante
    ● point-free
    ● etc

    View Slide

  9. ● 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)

    View Slide

  10. TODAS AS FUNÇÕES
    SÃO CURRIED

    View Slide

  11. const add = (x, y) => x + y
    add(1, 2); // 3
    add(1) // Error

    View Slide

  12. (curry)

    View Slide

  13. const curryAdd = R.curry((x, y) => x + y))
    curryAdd(1, 2); // 3
    const addOne = curryAdd(1); // Function
    addOne(2) // 3

    View Slide

  14. STATELESS
    FUNCTIONS
    todo list

    View Slide

  15. const Container = children => ({children});
    const List = children => ({children});
    const ListItem = ({ id, text }) => (
    {text}
    );

    View Slide

  16. const TodoItem = {
    id: 123,
    text: 'Comprar pão'
    };
    Container(List(ListItem(TodoItem)));
    Comprar pão

    View Slide

  17. COMPOSE HIGH
    ORDER COMPONENTS
    (pipe / compose)

    View Slide

  18. PIPE / COMPOSE:
    compõe funções de forma
    sequencial

    View Slide

  19. function1 function2

    View Slide

  20. Composed
    Function

    View Slide

  21. PIPE
    COMPOSE

    View Slide

  22. // Container(List(ListItem(TodoItem)));
    const TodoList = R.compose(
    Container,
    List,
    ListItem
    );
    TodoList(TodoItem);
    Comprar pão

    View Slide

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

    View Slide

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

    View Slide

  25. const sortedListItems = R.pipe(
    R.sort(byId),
    R.map(ListItems),
    )

    View Slide

  26. const TodoItems = [{...}, {...}, {...}]
    const TodoList = R.compose(
    Container,
    List,
    sortedListItems,
    );
    TodoList(TodoItems);

    View Slide

  27. COMPOSIÇAO DE
    PROMISE
    (pipeP / composeP)

    View Slide

  28. const todoList = R.pipeP(
    getUserById,
    getTodos,
    funçãoAsyncQueFazWhatever,
    ) // Promise
    await todoList(‘123’)

    View Slide

  29. DATA HANDLING
    (Evolve, ApplySpec)

    View Slide

  30. EVOLVE

    View Slide

  31. R.evolve({
    email: R.toLower,
    phone: R.replace(‘-’, ‘’),
    name: R.trim,
    })(data)

    View Slide

  32. // state {
    // …,
    // selected: false,
    // … ,
    // }
    onToggleSelected() {
    this.setState((prevState) => ({
    ...prevState
    selected: !prevState.selected
    }))
    }

    View Slide

  33. onToggleSelected() {
    this.setState(R.evolve({ selected: R.not }))
    }

    View Slide

  34. APPLY SPEC

    View Slide

  35. const createObj = R.applySpec({
    counter: R.inc,
    userData: { phone: R.trim},
    })
    createObj(0, “ 98499-1900”)
    // {1, userData{ phone: “9849919000”}}

    View Slide

  36. const handlePhone = R.pipe(
    R.split(‘-’),
    R.join,
    R.replace(/[^0-9]/, ‘’),
    algumaCoisa, // (str) => {str}
    )

    View Slide

  37. const createObj = R.applySpec({
    counter: R.inc,
    userData: { phone: handlePhone},
    })

    View Slide

  38. IMMUTABLE STATE
    (lenses)

    View Slide

  39. currentState -> newState
    0
    - +

    View Slide

  40. const counterLens = R.lensProp('counter');
    counterLens(state) //{ counter: 0 }

    View Slide

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

    View Slide

  42. // 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

    View Slide

  43. // sem lens :(
    this.setState((prevState) => {
    ({ counter: prevState.a.b.counter++ }))
    }
    // com lens :)
    this.setState(incCounter)

    View Slide

  44. render() {
    const count = getCounter(this.state)
    return ({count})
    }

    View Slide

  45. ● Se foca em apenas uma
    propriedade do objeto
    ● Não muta a propriedade
    ● Consigo lidar caso a propriedade
    é undefined
    ● Torna bem mais declarativo

    View Slide

  46. SWITCH CASE / IF
    (cond)

    View Slide

  47. switch (action.type) {
    case 'INCREMENT':
    return state + action.payload;
    case 'DECREMENT':
    return state - action.payload;
    default:
    return state;
    }

    View Slide

  48. const load = R.cond([
    [R.equals('INCREMENT'), R.inc(state)],
    [R.equals('DECREMENT'), R.dec(state)],
    [R.T, R.always(state)],
    ])
    load(action.type)

    View Slide

  49. const Content = (props) => {
    if(props.loading) {
    return
    }
    if(props.items.length == 0) {
    return
    }
    return
    }

    View Slide

  50. const Content = R.cond([
    [R.prop('loading'), Loading],
    [R.isEmpty(props.items), Missing],
    [R.T, Section],
    ])
    Content(props)

    View Slide

  51. STATIC COMPONENT
    (always)

    View Slide

  52. const Loading = () => (Loading)
    ReactDOM.render(, rootEl)

    View Slide

  53. const Loading = R.always(Loading)
    ReactDOM.render(, rootEl)

    View Slide

  54. STYLED
    COMPONENTS
    (path)

    View Slide

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

    View Slide

  56. const Name = () => styled.Text`
    font-weight: bold,
    font-size: 18px,
    color: ${R.path([‘theme’, ‘primaryColor’])}
    `

    View Slide

  57. ● memoize(Function)
    ● merge(List, Obj)
    ● tryCatch(Function)
    ● anyPass / AllPass(List)
    ● flatter(List)

    View Slide

  58. RAMDASCRIPT

    View Slide

  59. Nem sempre você tem um
    trade-off em legibilidade.

    View Slide

  60. 5 + 10 // 15
    R.add(5, 10) // 15
    [5, 10].reduce((x, y) => {x + y}, 0)
    [5, 10].reduce(add, 0)

    View Slide

  61. const obj = {x: 10}
    R.prop(‘x’)(obj) // 10
    obj.x // 10
    R.sortBy(R.prop(‘x’))
    R.prop(‘x’)({}) // undefined

    View Slide

  62. BOM TOOLBOX

    View Slide

  63. COOKBOOK

    View Slide

  64. ● 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.

    View Slide

  65. t.me/lambdastudygroup
    github.com/lambda-study-group

    View Slide

  66. OBRIGADA :)
    https://speakerdeck.com/anabastos
    anabastos.me

    View Slide