Skip to content

Instantly share code, notes, and snippets.

View FrankFang's full-sized avatar
🎯
Focusing

Frank Fang FrankFang

🎯
Focusing
View GitHub Profile

React lifecycle cheatsheet

Method Side effects1 State updates2 Example uses
Mounting
componentWillMount Constructor equivalent for createClass
render Create and return element(s)
componentDidMount DOM manipulations, network requests, etc.
Updating
componentWillReceiveProps Update state based on changed props
export imports
import d, {obj} from '...';
export {obj, d};
export {obj as name1, d as name2};
re-export all named imports
export * from '...';
export * as name1 from '...';
function on(node, eventType, selector, fn){
node.addEventListener(eventType, (e)=>{
let el = e.target
while (el && !el.matches(selector)) {
el = el.parentNode
if(node === el){ el = null }
}
if(el){ fn.call(el, e, el) }
})
@FrankFang
FrankFang / rails_options_for_select_i18n.rb
Created March 11, 2017 13:33
rails_options_for_select_i18n.rb
model:
class User < ActiveRecord::Base
ROLES = [
[I18n.t('role.admin'), 'admin'],
[I18n.t('role.manager'), 'manager'],
[I18n.t('role.user'), 'user']
]
end
locale:
en:
<!DOCTYPE html>
<title>这是一个合法的 HTML 5 文档</title>
<meta charset="UTF-8">
<p>这是一个合法的 HTML 5 文档
@FrankFang
FrankFang / start
Created January 13, 2017 05:51
@reboot auto run at start
https://askubuntu.com/questions/814/how-to-run-scripts-on-start-up/816#816
One approach is to add an @reboot cron task:
Running crontab -e will allow you to edit your cron.
Adding a line like this to it:
@reboot /path/to/script
will execute that script once your computer boots up.
@FrankFang
FrankFang / index.md
Created January 4, 2017 07:54 — forked from rstacruz/index.md
Rails models cheatsheet

Rails Models

Generating models

$ rails g model User

Associations

belongs_to

has_one

DOM Example

@FrankFang
FrankFang / square.coffee
Created June 20, 2016 08:02
Named function in CoffeeScript
# Classes are the only place CoffeeScript generates named functions.
# This might seem messy, but the extra wrapping is necessary in order
# to have consistent behavior in IE. See the FAQ for more details:
# https://github.com/jashkenas/coffee-script/wiki/FAQ
class square
constructor: (x) -> return x * x # constructors require an explicit return
console.log square 5 # 25
console.log square.name # square
@FrankFang
FrankFang / index.html.erb
Created June 15, 2016 08:07 — forked from lingceng/index.html.erb
Custom ransacker predicates to query date range
<%= search_form_for @q, url: production_status_changes_path, class: 'form-inline' do |f| %>
<%= f.label 'Create At' %>
<%= f.search_field :created_at_beginning_of_day, class: 'form-control input-sm', 'datepicker' => true %>
<%= f.search_field :created_at_end_of_day_lt, class: 'form-control input-sm', 'datepicker' => true %>
<% end %>