我想将此反应类组件转换为反应钩子组件。我转换了大部分代码,现在我需要帮助来转换渲染函数和返回函数之间的代码部分。
这是类组件:
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>
);
}
}
这是我创建的新反应钩子组件。正如我之前提到的,它还没有完成。
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>
);
}
这些是我转换代码所采取的步骤: 使用 Hooks 将 React 类组件转换为 React 函数式组件的 10 个步骤!