How I learned to stop worrying and love components
By @pepf
Dr Strangelove or How I Learned to Stop Worrying and Love the Bomb (1964) still
The movie actually bares no resemblence to the process I will talk about,
other than that we spend quite some time in meeting rooms
face
About me
Pepijn Fens (@pepf)
UX Designer & Frontend dev
Also this is my first meetup presentation, so bear with me
3
Why everything is ok now
I hope to answer the following three things during this presentation..
Current frontend structure
We started building the frontend we use now about 5 years ago.
It has evolved over time, started out with server side rendered pages,
gradually added more interaction with jquery, switched to backbone at some point
Codebase over time
This is quite typical for a backbone app.
As the app grows more complex,
more dependencies between components are created.
With every new feature, more connections are added to the dependency graph
delete_forever Clean slate
Maybe we should just start from scratch?
About a year ago we got the opportunity to look into more modern frameworks to speed up our development..
sentiment_dissatisfied
More questions
React, Vue, Angular?
Maintaining multiple codebases?
Rethinking the tooling we use
What about browser support?
How do we make sure we don't end up with this complexity again?
The last one is the most important, the one I think I can provide some help with during this talk.
extension Components
To make sure we won't mess it up again
But before jumping into code I want to start with why
I believe thinking in components can prevent us from getting into trouble again.
What exactly are components?And how we can use component thinking to create a clear application structure
Everybody here has already a notion of components
Atoms
Basic building blocks of matter
Molecules
Atoms bonded together to form functional pieces
Organisms
Distinct part of the application with it's own behaviour
Inspired by Atomic design by Brad Frost. Interesting methodology to break down a web page in individual pieces
Meant for designers, but concepts can be applied to development as well.
We start out with the smallest part...
Molecules
Functional piece made from atoms
For example a calendar component, can be used on various places in an application,
but as you can see it consists of smaller atoms and has a clear goal in itself.
Additionally, they should be build for reuse
Organisms
A clear & unique functional part of the application
Organisms consists of various types of molecules and atoms pulled together, creating a unique behaviour.
Takeaway: Categorizing helps in breaking down complex behaviour into reusable pieces.
Vue
Or, how to put this theory into practice
So now we're all up to speed regarding components..
Let's look at how Vue is actually the right tool for the job
Vue
Component based frontend library
"Best bits of angular and react combined"
Next I will show you why we chose vue over other frontend frameworks such as react.
Render logic
Vue
{{ buttonText }}
React
if (bigbutton) {
return ({ buttonText } );
}
So the first reason why we chose for Vue is the way it's templating syntax works.
If you compare it...
Transitions
Vue
Click here
.fade-enter-active, .fade-leave-active {
transition: opacity .5s
}
.fade-enter, .fade-leave-active {
opacity: 0
}
Class and style binding
Click here
data: {
classes: {
active: true,
primary: false
}
}
Events
Default events
Click here
Custom events
Click here
Component <specialButton>
JS
....
Internal state
Methods
HTML
<button :class=”classes” @click=”doThings”> {{ buttonText }} </button>
CSS
Or LESS / SASS / SCSS
Props
Events
Component <specialForm>
<specialButton buttonText="Click here"></specialButton>
Now that we know a bit more about vue's templating language, well look more closely into what exactly is a component?
- isolated piece of the application
- three parts: template, internal logic and optionally styling
- "one big tree"
- communication with parent: props down, events up
restore_page Application state
How do all these components work together?
Now we know the advantages of components and how they can be categorized by complexity,
it's time to talk about how components work together to create a functional application
Vuex
Actions and mutations change state
State is ALL the data required to render a specific page
Smart
Rely on global state
"Wrapper" components
Dumb
Don't rely on global state
"Visual" components
Interaction with components
Store (application state)
{ posts: [ ..., ...], user: { name: ‘sdfasd’, id: 1, avatar: ‘img/awesome.jpg’ } }
UserInfo
Avatar
Post
PostList
Post
Post
Connecting to the state
export default {
name: 'UserInfo',
store,
getters: {
user: state => { return state.user }
},
methods: {
onClick () {
this.$store.dispatch('logout')
}
}
}
Code example in Vue how state getters work
This is just a really short example of where components get their data from
I recommend you to look at the docs if you want to know more
const store = new Vuex.Store({
// All application data
state: {
user: {id:1, name: "henk", avatar: 'me.jpg'},
posts: []
},
// Called from the store
mutations: {
logoutMutation (state) {
state.user = {}
}
},
// Called from the component
actions: {
logout (context) {
context.commit('logoutMutation')
}
}
})
There's always just one store, but all elements are able to be split off
to keep the store structured
Something about webpack & babel
When working on our new frontend we also moved towards webpack and babel.
Takes some time to set up, but once it's running it feels like..
hot_tub
Advantages
Or, how components make your life easier
view_module
Reusability
Ensures consistent look&feel and prevents double code
Additonally, leverage reusability of components to create living styleguide
Living style guide.
Lives in a separate repository, using our shared components package.
Collaborative effort between designers & developers. Designer document the interactions of components, while devellpers document the different props and validations in clear dev code examples.
textsms
Semantics
Common language to talk about components with techs and non-techs alike
all_inclusive
Maintainability
Enforce coding style that allows unit testing and iterative development
import myButton from 'myButton'
var Button = Vue.extend(myButton)
describe('myButton component', function () {
it('should set the proper class', () => {
// 1. Create the component
var vm = new Button({
propsData: {
type: 'bigbutton'
}
}).$mount()
// 2. Run your tests (Chai.js notation)
const buttonEl = vm.$el.querySelector('button')
expect(buttonEl).to.be.true
expect(buttonEl.className).to.contain('bigbutton')
// 3. Destroy the component
vm.$destroy();
})
})
Thanks for listening!
help_outline
Questions?
Closing words: Whether you are creating a small hobby project for yourself or laying foundations for a bigger SPA for
your startup, Vue is able to handle it because of the isolated components.
State management and it's complexities is too big to cover here, but I recommend you to read into it, because it's really powerful.
Can discuss this with people after the presentation