I want to convert this react class component into a react hooks component. I converted most of the code and now I need help to convert the part of code that is between the render and the return functions.
Here is the class component:
class LoginGoogle extends React.Component {
state = {
loading: true,
error: null,
data: {},
};
componentDidMount() {
fetch(`/api/auth/google/callback${this.props.location.search}`, { headers: new Headers({ accept: 'application/json' }) })
.then((response) => {
if (response.ok) {
return response.json();
}
throw new Error('Something went wrong!');
})
.then((data) => {
this.setState({ loading: false, data });
})
.catch((error) => {
this.setState({ loading: false, error });
console.error(error);
});
}
render() {
const { loading, error, data } = this.state;
if (loading) {
return <Layout>Loading....</Layout>;
}
if (error) {
return (
<Layout>
<div>
<p>Error:</p>
<code className="Code-block">{error.toString()}</code>
</div>
</Layout>
);
}
return (
<Layout>
<div>
<details>
<summary>Welcome {data.user.name}</summary>
<p>Here is your info: </p>
<code className="Code-block">{JSON.stringify(data, null, 2)}</code>
</details>
</div>
</Layout>
);
}
}
And this is the new react hooks component that I created. As I mentioned before it's still not finished yet.
function LoginGoogle (props) {
const [start, setStart] = useState(
{
loading: true,
error: null,
data: {},
}
)
useEffect(() => {
fetch(`/api/auth/google/callback${props.location.search}`, { headers: new Headers({ accept: 'application/json' }) })
.then((response) => {
if (response.ok) {
return response.json();
}
throw new Error('Something went wrong!');
})
.then((data) => {
setStart({ loading: false, data });
})
.catch((error) => {
setStart({ loading: false, error });
console.error(error);
});
})
const { loading, error, data } = this.state;
if (loading) {
return <Layout>Loading....</Layout>;
}
if (error) {
return (
<Layout>
<div>
<p>Error:</p>
<code className="Code-block">{error.toString()}</code>
</div>
</Layout>
);
}
return (
<Layout>
<div>
<details>
<summary>Welcome {data.user.name}</summary>
<p>Here is your info: </p>
<code className="Code-block">{JSON.stringify(data, null, 2)}</code>
</details>
</div>
</Layout>
);
}
These are the steps that I took to convert the code : 10 Steps to Convert React Class Component to React Functional Component with Hooks!