giovictor
12/3/2019 - 7:28 AM

React Hooks

useState - equivalent of setting a state object and using this.setState({})

Example:

const [posts, setPosts] = useState([])
/* where the argument of useState is the initial value of the state */
/* to set the state */
setPosts(newPosts)

useEffect - equivalent of setting a lifecycle method/hook

Example:

useEffect(() => {
  /* some code here */
}, [])

/* first argument is a function and second is an array. 
The function executes once the value in the array changes. */

REDUX HOOKS

useSelector - equivalent of mapStateToProps

Example:

const posts = useSelector(state => state.posts)

useDispatch - equivalent of mapDispatchToProps

Example:

const dispatch = useDispatch()
/* used in useEffect() or any method you like */
useEffect(() => {
  dispatch({type:"ALL_POSTS"}) /* argument will be the actions return object */
}, [])