Skip to content

Instantly share code, notes, and snippets.

View hacker0limbo's full-sized avatar
:electron:
Where the fuck I am

Limboer hacker0limbo

:electron:
Where the fuck I am
  • eBay
  • 12:52 (UTC +08:00)
View GitHub Profile
const factorial = n => {
if (n == 0 || n == 1) {
return 1
}
return n * factorial(n-1)
}
@hacker0limbo
hacker0limbo / utils.js
Last active June 6, 2019 23:12
一些常用的函数
const currentTime = () => {
const d = new Date()
const year = d.getYear()
const month = d.getMonth() + 1
const date = d.getDate()
const hours = d.getHours()
const minutes = d.getMinutes()
const seconds = d.getSeconds()
const timeString = `${year}/${month}/${date} ${hours}:${minutes}:${seconds}`
@hacker0limbo
hacker0limbo / counter_hooks.js
Created August 13, 2019 13:01
A Counter using useReducer and useContext hooks
import React, { useReducer, useContext, createContext } from 'react'
import ReactDOM from 'react-dom'
const initialState = {
count: 0
}
const CounterContext = React.createContext()
const counterReducer = (state, action) => {
@hacker0limbo
hacker0limbo / useReducer_useRef_demo.js
Created August 14, 2019 11:18
use useReducer and useRef hooks to create a counter and todo example
import React, { useReducer, useRef } from 'react'
import ReactDOM from 'react-dom'
const Counter = () => {
const [count, dispatch] = useReducer((state, action) => {
switch(action) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
@hacker0limbo
hacker0limbo / useFormInput.js
Created August 17, 2019 19:29
custom hooks from Dan Abramov's speech
const useFormInput = initialValue => {
const [value, setValue] = useState(initialValue)
const handleChange = e => {
setValue(e.target.value)
}
return {
value,
onChange: handleChange
@hacker0limbo
hacker0limbo / cookie和session小节.md
Created August 22, 2019 12:22
session 和 cookie 模拟
@hacker0limbo
hacker0limbo / mongodb.md
Last active August 22, 2019 12:33
软件安装的一些操作

mongodb

安装

使用homebrew安装, 安装以后需要配置环境变量, 在.bash_profile.zshrc里面加上export PATH=/usr/local/Cellar/mongodb/4.0.3_1/bin:$PATH, 然后source使文件生效. 同时需要在 root 目录下新建数据目录, 如下:

mkdir -p /data/db
@hacker0limbo
hacker0limbo / counter_react-redux.js
Last active August 22, 2019 13:01
使用 react-redux, redux 实现的计数器
/*
使用 react-redux 实现的计数器
*/
import React from 'react'
import ReactDOM from 'react-dom'
import { connect, Provider } from 'react-redux'
import { createStore } from 'redux'
const mapStateToProps = state => {
@hacker0limbo
hacker0limbo / readmore_useState.js
Last active August 22, 2019 13:03
使用 useState 实现 readmore 效果
import React, { useState } from 'react'
import ReactDOM from 'react-dom'
const text = 'To see the answers, we need to take a step back. The goal of this article isn’t to give you a list of bullet point recipes. It’s to help you truly “grok” useEffect. There won’t be much to learn. In fact, we’ll spend most of our time unlearning.'
const RevealText = props => {
const { text, maxLength } = props
const [hidden, setHidden] = useState(true)
if (text.length <= maxLength) {
@hacker0limbo
hacker0limbo / 闭包.md
Created August 24, 2019 05:20
一些概念的总结(前端)

重点:

  • 可以返回一个对象, 或者一个函数
  • 闭包是一个有状态(不消失的私有数据)的函数
  • 闭包是一个有记忆的函数
  • 闭包相当于一个只有一个方法的紧凑对象

示例 1, 非闭包:

// 无论调用多少次 fakeCounter(), 永远返回的是 0 
const fakeCounter = function() {