我很难理解为什么create-react-app无法编译,并告诉我error 'updateWord' is not defined no-undef。我对ES6的反应很新鲜。通常,我会编写一个像const App = React.createClass({ });这样的组件,但是我决定尝试一些语法糖。
我有父组件App和子组件Input
class App extends Component {
constructor(props) {
super(props);
// all other code omitted...
}
handleIn
我试图找出在React组件中this绑定中某些奇怪行为的原因。
我习惯于开发组件并将它们的方法放在类主体中,并将它们绑定到constructor中的组件的constructor中。但是,我最近决定通过提取一些大型方法来分离文件,然后将它们导入到组件中,从而清理和分离关注点。
令我沮丧的是,在这种情况下,绑定this并不那么容易。更奇怪的是,虽然使用ES6箭头函数似乎没有正确绑定,但基本ES5函数的使用很好。我想找出这种行为的原因:
示例1按预期工作。
import React, { Component } from 'react';
class App extends Com
我想在同一个类中调用该方法。例如,当我单击一个按钮时,它将触发方法handleLoginBtnClicked()。我希望它会在同一个类中调用方法checkInputValidation()。做这件事的正确方法是什么?
export default class LoginCard extends React.Component {
//If I click a button, this method will be called.
handleLoginBtnClicked() {
this.checkInputValidation();
}
import React, {Component} from "react";
import {connect} from "react-redux";
import PropTypes from "prop-types";
import "../App.css";
import {register} from "../actions/authActions"
class Register extends Component {
state = {
username: "",
我在React (或一般的JS )中理解整个this问题时遇到了一些问题,并发现这篇非常有用的文章:
然而,有一件基本的事情我仍然不确定。
让我们以方法2为例:
// Approach 2: Bind in Render
class HelloWorld extends React.Component {
constructor(props) {
super(props);
this.state = { message: 'Hi' };
}
logMessage() {
// This works because of the bind