Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

javascript - 当从父组件传递点击时,为什么未为子组件定义“ this”?(Why 'this' is undefined for child component, when click is passed from a parent component?)

Trying to educate in React.(尝试在React中进行教育。)

It might be the case, that the whole structure is wrong however, this is it:(可能是这样,整个结构是错误的,是这样的:) LoginComponent contains LoginForm, and passes onSubmit event down to the form.(LoginComponent包含LoginForm,并将onSubmit事件传递到表单。) The submitForm triggers an actionCreator, that is why I have to use this.props.login .(SubmitForm会触发一个actionCreator,这就是为什么我必须使用this.props.login 。) But when the call is happening this is undefined .(但是当呼叫发生时, thisundefined 。) I'm doing this, since LoginComponent will become an Auth component and contain the registration form as well.(我正在执行此操作,因为LoginComponent将成为Auth组件,并且还包含注册表格。) But is it correct in general?(但这是正确的吗?) import React from 'react'; import {connect} from "react-redux"; import {userActions} from "../../actions/auth.actions"; import LoginForm from "./loginForm"; class LoginComponent extends React.Component { constructor(props) { super(props); this.state = { username: '', password: '' } } submitForm(username, password) { this.props.login(username, password); } render() { return ( <> <LoginForm onSubmit={this.submitForm}/> </> ); } } const mapStateProps = state => { const {isLoggedIn, isLoggingIn, user} = state; return {isLoggedIn, isLoggingIn, user}; }; const actionCreators = { login: userActions.login, }; const connectedLoginComponent = connect(mapStateProps, actionCreators)(LoginComponent); export {connectedLoginComponent as Login}; LoginForm:(登录表格:) import React, {useState} from 'react'; import PropTypes from 'prop-types'; const LoginForm = (props) => { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const { onSubmit } = props; const handleSubmit = (e) => { e.preventDefault(); onSubmit(username, password); }; return ( <> <form onSubmit={e => handleSubmit(e)}> <input onChange={e => setUsername(e.target.value)} value={username} name={"username"} type="text" placeholder={'Username'} required /> <input onChange={e => setPassword(e.target.value)} value={password} name={"password"} type="text" placeholder={'Password'} required /> <button>Login</button> </form> </> ); }; LoginForm.propTypes = { onSubmit: PropTypes.func, }; export default LoginForm;   ask by Animus translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The function is not having this access we have to explicitly bind this to function or we can use arrow function.(该功能是没有this访问,我们必须明确地绑定this以功能或者我们可以使用箭头功能。)

Arrow function :(箭头功能 :) submitForm = (username, password) => { this.props.login(username, password); } Bind keep this in your constructor :(将其绑定在您的构造函数中:) this.submitForm = this.submitForm.bind(this)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...