reactjs - Error: Identifier already been declared. How can it be resolved?
问题描述
I am using Grapgql for querying in React application. I have a query & mutation, both in the same component. Because of this, the variables defined error & data is repeated. Therefore it is showing an error in React as Identifiers already declared
Below is the code for the component:
function DisplayUsers() {
let { id } = useParams();
const { loading, error, data } = useQuery(LOAD_USERS_BY_ID, {
variables: { id: parseInt(id) },
});
const [deleteUserById, { error, data }] = useMutation(DELETE_USER);
const deleteUser = () => {
deleteUserById({ variables: { id: id } });
};
useEffect(() => {
if (data) {
console.log(data);
alert('Successful deletion');
setTimeout(() => {
<Redirect
to={{
pathname: '/view-users',
}}
/>;
}, 3000);
}
}, [data]);
console.log(typeof id);
console.log(data);
if (loading) return <h4>Loading data...</h4>;
return (
data && (
<div className='row'>
<div className='col s12 m6 offset-m3' style={{ marginTop: '50px' }}>
<div className='card'>
<div className='card-content'>
<span className='card-title'>
{data.getUsersById.firstName} {data.getUsersById.lastName}
</span>
<p>Registerd Email: {data.getUsersById.email}</p>
<p>Registerd Password: {data.getUsersById.password}</p>
<p>User Id : {data.getUsersById.id}</p>
<br />
<div className='card-action'>
<button
onClick={deleteUser}
className='waves-effect waves-light btn-small right'
>
Delete
</button>
</div>
</div>
</div>
</div>
</div>
)
);
}
How can it be overcome?
解决方案
You can provide different names like this
const { loading, error: userError, data: userData } = useQuery(LOAD_USERS_BY_ID, {
variables: { id: parseInt(id) },
});
then use the new names in the rest of the code
OR
const userQuery = useQuery(LOAD_USERS_BY_ID, {
variables: { id: parseInt(id) },
});
then use userQuery.data
in rest of the code
推荐阅读
- mysql - 如何将列中的所有值修改为固定的 9 位数字以及每个以常量 int 405 开头的结果,例如 51=405000051
- ubuntu - 错误:我们找到了 libevent 的库,但找不到 C 头文件
- ios - WKWebview 显示带有填充的文本
- r - Shiny 上下文中的 ggplotly 问题
- sql - Oracle:检索不到两年前具有 upd_ts 的记录
- python - TF 预处理:分类值的插补类
- angular - 在 Angular 2 中抓取网站之前,如何(动态地)更改元标记?
- sql - 如何 SQL 查询具有另一个属性的更具体变化的元素?
- mysql - xaamp MySql 服务器无法启动
- r - 在目录中创建文件列表,对每个文件应用(lapply)自定义函数,并将结果 cbind 到新文件