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
  • 22:37 (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 / 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 / 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 / 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 / quote.md
Last active October 13, 2019 15:47
一些语录

1

面向对象编程的问题是,每个对象都有自己的状态,开发程序时,必须记住当前所有对象的状态。

为了让我们的生活更轻松,最好只有一小部分代码库处理状态,其他代码都是无状态和纯的。实际上,这就是前端的 Redux 库取得巨大成功的主要原因。

-- 《面向对象编程:一万亿美元的错误》

2 React 应该尽量遵守 "有状态的组件没有渲染, 有渲染的组件没有状态" 这一原则, 这句话的意思是:

@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 / cookie和session小节.md
Created August 22, 2019 12:22
session 和 cookie 模拟