使用homebrew
安装,
安装以后需要配置环境变量, 在.bash_profile
和.zshrc
里面加上export PATH=/usr/local/Cellar/mongodb/4.0.3_1/bin:$PATH
, 然后source
使文件生效.
同时需要在 root 目录下新建数据目录, 如下:
mkdir -p /data/db
const factorial = n => { | |
if (n == 0 || n == 1) { | |
return 1 | |
} | |
return n * factorial(n-1) | |
} |
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}` |
import React, { useReducer, useContext, createContext } from 'react' | |
import ReactDOM from 'react-dom' | |
const initialState = { | |
count: 0 | |
} | |
const CounterContext = React.createContext() | |
const counterReducer = (state, action) => { |
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 |
/* | |
使用 react-redux 实现的计数器 | |
*/ | |
import React from 'react' | |
import ReactDOM from 'react-dom' | |
import { connect, Provider } from 'react-redux' | |
import { createStore } from 'redux' | |
const mapStateToProps = state => { |
const useFormInput = initialValue => { | |
const [value, setValue] = useState(initialValue) | |
const handleChange = e => { | |
setValue(e.target.value) | |
} | |
return { | |
value, | |
onChange: handleChange |
1
面向对象编程的问题是,每个对象都有自己的状态,开发程序时,必须记住当前所有对象的状态。
为了让我们的生活更轻松,最好只有一小部分代码库处理状态,其他代码都是无状态和纯的。实际上,这就是前端的 Redux 库取得巨大成功的主要原因。
2 React 应该尽量遵守 "有状态的组件没有渲染, 有渲染的组件没有状态" 这一原则, 这句话的意思是:
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) { |