useEffect() sẽ giống hệt với componentDidMount

https://viblo.asia/p/su-dung-useeffect-trong-reacthooks-1Je5EPbmlnL

import React, {useEffect, useState} from 'react';
const EffectDemo = () => {
  const [fullName, setFullName] = useState({ name: 'name', familyName: 'family' });
  const [title, setTitle] = useState('useEffect() in Hooks');
  useEffect(() => {
  console.log('useEffect has been called!');
	  setFullName({ name: 'TrungHC', familyName: 'HCT' });
	}, [fullName.name, fullName.familyName]);
  return (
    <div>
        <h1>Title: {title}</h1>
        <h3>Name: {fullName.name}</h3>
        <h3>Family Name: {fullName.familyName}</h3>
    </div>
  );
};
export default EffectDemo;

Last updated

Was this helpful?