javascript - OnClick Javascript函数在reactjs中不起作用
问题描述
我有一个登录页面,下面显示了我的登录页面的 jsx。登录提交按钮在此处。单击此登录按钮时,不会触发任何更改或没有反映任何更改
const SectionLogin = ({ onLoginClick, setEmail, setPassword }) => {
return (
<>
<div
className='wrapper'
style={{
backgroundImage: 'url(' + require('app/assets/img/j.jpg') + ')',
}}
>
<Container>
<Row>
<Col
className='mx-auto'
lg='4'
md='6'
style={{ paddingTop: '60px' }}
>
<Card className='card-register' style={{ padding: '15px' }}>
<h3 className='title mx-auto'>Welcome</h3>
<div className='social-line text-center'>
<Button
className='btn-neutral btn-just-icon mt-0'
color='facebook'
// onClick={(e) => e.preventDefault()}
>
<i
className='fab fa-facebook fa-3x'
style={{ color: '#3b5998' }}
/>
</Button>
<Button
className='btn-neutral btn-just-icon mt-0 ml-1'
color='google'
// onClick={(e) => e.preventDefault()}
>
<i
className='fab fa-google-plus fa-3x'
style={{ color: '#db4a39' }}
/>
</Button>
<Button
className='btn-neutral btn-just-icon mt-0 ml-1'
color='twitter'
// onClick={(e) => e.preventDefault()}
>
<i
className='fab fa-twitter fa-3x'
style={{ color: '#00acee' }}
/>
</Button>
</div>
<Form className='register-form'>
<label>Email</label>
<InputGroup className='form-group-no-border'>
<InputGroupAddon addonType='prepend'>
<InputGroupText>
<i className='nc-icon nc-email-85' />
</InputGroupText>
</InputGroupAddon>
<Input
placeholder='Email'
type='email'
// onChange={(e) => setEmail(e.target.value)}
/>
</InputGroup>
<label>Password</label>
<InputGroup className='form-group-no-border'>
<InputGroupAddon addonType='prepend'>
<InputGroupText>
<i className='nc-icon nc-key-25' />
</InputGroupText>
</InputGroupAddon>
<Input
placeholder='Password'
type='password'
// onChange={(e) => setPassword(e.target.value)}
/>
</InputGroup>
<Button
block
className='btn-round'
color='warning'
type='button'
onClick={onLoginClick}
>
Login
</Button>
</Form>
<div className='forgot'>
<Button
className='btn-link'
color='secondary'
// onClick={(e) => e.preventDefault()}
>
Forgot password?
</Button>
</div>
</Card>
<div className='col text-center'>
{/* <Button
className='btn-round'
outline
color='neutral'
href='/register-page'
size='lg'
target='_blank'
>
View Register Page
</Button> */}
</div>
</Col>
</Row>
</Container>
</div>
</>
);
};
export default SectionLogin;
按钮代码在此 SectionLogin 容器内(在我的登录页面 jsx 所在的返回内)
import React, { useEffect, useCallback, useState } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import SectionLogin from './LoginPage';
const LoginContainer = () => {
const dispatch = useDispatch();
const onLoginClick = (e) => {
console.log('hereeee');
alert('Hello');
};
return (
<>
<div className='content'>
<SectionLogin
onLoginClick={onLoginClick}
/>
</div>
</>
);
};
export default LoginContainer;
单击按钮时不会触发 onLoginClick 函数。
解决方案
你可以试试这个,
<Button
block
className="btn-round"
color="warning"
type="button"
onClick={(event)=>onLoginClick(event)}>
Login
</Button>;
希望这可以帮助!!
推荐阅读
- ios - 创建具有固定大小和自定义图标的 UIBarButtonItem
- python - 为什么 python 在使用 ctypes 加载 dll 时会引发错误 OSError?
- jira - JIRA 在上个月的报告中花费了时间
- angular - 基于 ResponseHeaders 解析响应
- ios - Swift ios如何设置导航栏图像不拉伸
- html - 为什么 height 100% 不能将 div 完全扩展到页面底部?
- jwt - 无法为 Apollo 客户端设置身份验证标头
- java - 查询DSL 4.1.4,Jpa GROUP BY Date
- c# - WriteableBitmap 周围不需要的红色边框
- java - 扫描文档中的某些文本,替换某些单词(这些单词应该是变量)并使用 Java 中的 Apache POI 保存在新文档中