简体   繁体   English

为什么我在 React 中的简单名称更改 function 不起作用?

[英]Why does my simple name change function in React not work?

 import React, { Component } from "react"; import LogIn from "./LogIn.js" import User from "./components/User.js" class App extends Component { constructor(props) { super(props); this.state = { projectList: [], myProjects: [], userList: [], currentUser: "Guest" }; this.logIn = this.logIn.bind(this); } logIn(name) { this.setState({ currentUser: name }) } render() { return( <div> <User name = {this.state.currentUser}/> <LogIn logIn = {this.logIn} /> </div> ); } } export default App;

 import React from "react"; class LogIn extends React.Component { constructor(props) { super(props); this.state = { name: "" }; this.handleSubmit = this.handleSubmit.bind(this); this.handleInputChange = this.handleInputChange.bind(this); } handleSubmit(event) { event.preventDefault(); this.props.logIn( this.state.name ) } handleInputChange(event) { const target = event.target; const value = target.value; const name = target.name; this.setState({ [name]: value }); } render() { return ( <div> <h2> Login: </h2> <form onSubmit={this.handleSubmit}> <label> Name: <input name="name" type="textbox" checked={this.state.name} onChange={this.handleInputChange} /> </label> <br /> <input type="submit" value="Login" /> </form> </div> ); } } export default LogIn;

 import React, { Component } from "react"; class User extends Component { constructor(props) { super(props); this.state = { name: props.name, } } render() { return ( <div> <p> Current User: {this.state.name}</p> </div> ) } } export default User;

Here, I have a simple App, which gives users a login form, that allows them to change their name.在这里,我有一个简单的应用程序,它为用户提供了一个登录表单,允许他们更改姓名。 The User class stores the name that App passes it and renders it.用户 class 存储 App 传递它并呈现它的名称。 However, when I submit a name in the login form, no name change is rendered.但是,当我在登录表单中提交名称时,不会呈现名称更改。 Guest is shown throughout the entire process.客人在整个过程中都会出现。 This leads me to believe that currentUser is not changing.这让我相信 currentUser 没有改变。 Can anyone tell me why the currentUser is unchanged after logIn is called?谁能告诉我为什么调用 logIn 后 currentUser 没有改变?

In User the constructor run only one time when the component re-render.User中,构造函数仅在组件重新渲染时运行一次。 When component re-render constructor is not called, so this.state.name will not updated when component re-render.当不调用组件重新渲染constructor时,组件重新渲染时this.state.name不会更新。 You do not need to set state for name, just render with this.props.name , the name in UI will be updated.您不需要为名称设置 state ,只需使用this.props.name进行渲染,UI中的名称将被更新。

 import React, { Component } from "react"; class User extends Component { constructor(props) { super(props); } render() { return ( <div> <p> Current User: {this.props.name}</p> </div> ) } } export default User;

If you really want to have state for name, you need to update the state in componentDidUpdate如果你真的想要 state 作为名字,你需要在componentDidUpdate中更新 state

 import React, { Component } from "react"; class User extends Component { constructor(props) { super(props); this.state = { name: props.name, } } componentDidUpdate(prevProps){ if(prevProps.name.== this.props.name){ this:setState({name. this.props:name}) } render() { return ( <div> <p> Current User. {this.state;name}</p> </div> ) } } export default User;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM