简体   繁体   English

在 React 中重新渲染 class

[英]Re-render class in React

new to React, I'm testing to write a small program where you select a name in a list, and then a proverb should be displayed. React 新手,我正在测试编写一个小程序,在其中您 select 列表中的名称,然后应显示谚语。 The event handler handleAuthorClick is called, and setState is also called, but the new text is not displayed.事件处理程序handleAuthorClick 被调用,setState 也被调用,但是新的文本不显示。

Why?为什么?

class AuthorList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: -1,
      text: "When in Rome do as the Romans",
    }
  }

  handleAuthorClick()
  {
       let txt = "initial value";
       var sel = document.getElementById("author-list"); 
       switch (parseInt(sel.value))
        {
          case 12 : txt = "When in Rome do as the Romans"; break;
          case 33: txt = "If you tell the truth, you don't have to remember anything.";break;
          case 256: txt = "History will be kind to me for I intend to write it";break;
          default:;
        }
       this.setState({text: txt});
  }

  render() {
      return (
        <div className="author-list">
          <h2>Author List</h2>
          <select id='author-list' name='author-list' size='15' onClick={() => this.handleAuthorClick()>
             <option value="12">Unknown</option>
             <option value="33">Mark Twain</option>
             <option value="256">Winston Churchill</option>
          </select>
          <h2>Proverbs</h2>
          {this.props.text}
        </div>
      );
    }
  }

class ProVerbMain extends React.Component {
      render() {
      return (
        <div className="proverb">
          <h1>Dans proverbs</h1>
          <div className="author-list">
            <AuthorList text = 'Initial value'/>
          </div>
        </div>
      );
    }
  }

ReactDOM.render(<ProVerbMain />, document.getElementById('root'));

Use this.state.text instead of this.props.text , because it's a local state of your component.使用this.state.text而不是this.props.text ,因为它是组件的本地 state。 When you use this.props.text your component doesn't know the new state for new render.当您使用this.props.text您的组件不知道新的 state 用于新的渲染。 You should use onChange() event instead of onClick() in select option您应该在 select 选项中使用onChange()事件而不是onClick()

this should work for you, use onChange rather than onClick .这应该对你有用,使用onChange而不是onClick also you were using class methods handleAuthorClick() and not even binding them to class's this as function have their own this scope, you can't access class members from inside that function, that's why either use () => {} or use .bind(this) in class's constructor also you were using class methods handleAuthorClick() and not even binding them to class's this as function have their own this scope, you can't access class members from inside that function, that's why either use () => {} or use .bind(this)在类的构造函数中.bind(this)

EDIT: https://reactjs.org/docs/forms.html#the-select-tag编辑: https://reactjs.org/docs/forms.html#the-select-tag

import React, { useEffect } from "react";
import ReactDOM from "react-dom";

import "./styles.css";
class AuthorList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: -1,
      text: "When in Rome do as the Romans"
    };
  //this.handleAuthorClick = this.handleAuthorClick.bind(this); 
  }

  handleAuthorClick = e => {
    let txt = this.props.text;
    console.log(e.target.value);
    switch (e.target.value) {
      case "12":
        txt = "When in Rome do as the Romans";
        break;
      case "33":
        txt = "If you tell the truth, you don't have to remember anything.";
        break;
      case "256":
        txt = "History will be kind to me for I intend to write it";
        break;
      default:
    }
    // alert(txt);
    this.setState({ text: txt });
  };

  render() {
    return (
      <div className="author-list">
        <h2>Author List</h2>
        <select name="author-list" size="15" onChange={this.handleAuthorClick}>
          <option value="12">Unknown</option>
          <option value="33">Mark Twain</option>
          <option value="256">Winston Churchill</option>
        </select>
        <h2>Proverbs</h2>
        {this.state.text}
      </div>
    );
  }
}

class ProVerbMain extends React.Component {
  render() {
    return (
      <div className="proverb">
        <h1>Dans proverbs</h1>
        <div className="author-list">
          <AuthorList text="Initial value" />
        </div>
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<ProVerbMain />, rootElement);

demo: https://codesandbox.io/s/react-hooks-counter-demo-yj3s5演示: https://codesandbox.io/s/react-hooks-counter-demo-yj3s5

You've to use state instead of props.您必须使用 state 而不是道具。

import React from "react";

class AuthorList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: -1,
      text: "When in Rome do as the Romans"
    };
  }

  handleAuthorClick() {
    let txt = "initial value";
    var sel = document.getElementById("author-list");
    switch (parseInt(sel.value, 10)) {
      case 12:
        txt = "When in Rome do as the Romans";
        break;
      case 33:
        txt = "If you tell the truth, you don't have to remember anything.";
        break;
      case 256:
        txt = "History will be kind to me for I intend to write it";
        break;
      default:
    }
    //alert(txt);
    this.setState({ text: txt });
  }

  render() {
    const { text } = this.state;
    return (
      <div className="author-list">
        <h2>Author List</h2>
        <select
          id="author-list"
          name="author-list"
          size="15"
          onClick={() => this.handleAuthorClick()}
        >
          <option value="12">Unknown</option>
          <option value="33">Mark Twain</option>
          <option value="256">Winston Churchill</option>
        </select>
        <h2>Proverbs</h2>
        {text}
      </div>
    );
  }
}

class ProVerbMain extends React.Component {
  render() {
    return (
      <div className="proverb">
        <h1>Dans proverbs</h1>
        <div className="author-list">
          <AuthorList text="Initial value" />
        </div>
      </div>
    );
  }
}

export default ProVerbMain;

And here is your working sandbox link https://codesandbox.io/s/still-grass-r5iz9/这是您的工作沙箱链接https://codesandbox.io/s/still-grass-r5iz9/

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

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