简体   繁体   English

反应:调用两个事件处理程序onClick并更改子组件的样式

[英]React: Calling two event handlers onClick and changing style of child component

I have a parent class-based component A and a child functional component B . 我有一个基于父类的组件A和一个子功能组件B Inside B I map over a list of names and render them as li elements, which onClick call the onLanguageUpdate handler declared in the parent component, and what this handler does is update the state to reflect the selected name. B内部,我映射了一个名称列表并将其呈现为li元素, onClick调用在父组件中声明的onLanguageUpdate处理程序,并且该处理程序所做的是更新状态以反映所选名称。

Question then : 然后问题

I need to call a second event handler in the same onClick , this time to change the color of the name the user has clicked on. 我需要在同一onClick调用第二个事件处理程序,这一次是为了更改用户单击的名称的颜色。 I added a new property to the state, color , to represent a className that I can then toggle with the handleStyleColorChange handler. 我向状态color添加了一个新属性,以表示className ,然后可以使用handleStyleColorChange处理程序进行切换。 But how do I get the li elements in the child component to update their className (or style) based on the result of this handler? 但是,如何根据此处理程序的结果获取子组件中的li元素以更新其className(或样式)? If I was doing all of this inside component A's render method, I could do style={language === this.state.selectedLanguage ? {color: 'red'} : null} 如果我在组件A的render方法中进行了所有这些操作,则可以使用style={language === this.state.selectedLanguage ? {color: 'red'} : null} style={language === this.state.selectedLanguage ? {color: 'red'} : null} on the li and call it a day. li上的style={language === this.state.selectedLanguage ? {color: 'red'} : null}并称之为一天。

// Component A
import React, { Component } from 'react';
import B from './B';

class A extends Component {
  constructor(props) {
    super(props);

    this.state = {
      selectedLanguage: 'All',
      color: 'lang-black-text'
    };
  }

  handleUpdateLanguage = (language) => {
    return this.setState({ selectedLanguage: language });
  }

  handleStyleColorChange = (language) => {
    if (language === this.state.selectedLanguage) {
      return this.setState({ color: 'lang-red-text' });
    } else {
      return this.setState({ color: 'lang-black-text' });
    }
  }

  handleClick = (language) => {
    this.handleUpdateLanguage(language);
    this.handleStyleColorChange(language);
  }

  render() {

    return (
      <LanguageList onLanguageUpdate={this.handleClick} />
    );
  }
}

export default A;

// Component B
import React from 'react';

const B = (props) => {
  const languages = ['English', 'Spanish', 'Japanese', 'Italian'];

  const languageListFormatted = languages.map(language => {
    return (
      <li
        key={language}
        onClick={() => props.onLanguageUpdate(language)}>{language}
      </li>
    );
  });

  return (
    <ul className="languages">{languageListFormatted}</ul>
  );
}

export default B;

You can't manage the color from the parent comp, it needs to be done from the child comp. 您无法通过父组件管理颜色,而需要通过子组件进行管理。 Then, send the selectedLanguage to the child and you are good. 然后,将selectedLanguage发送给孩子,您就很好了。

 class A extends React.Component { constructor(props) { super(props); this.state = { selectedLanguage: 'All', color: 'lang-black-text' }; } handleUpdateLanguage = (language) => { return this.setState({ selectedLanguage: language }); } handleStyleColorChange = (language) => { if (language === this.state.selectedLanguage) { return this.setState({ color: 'lang-red-text' }); } else { return this.setState({ color: 'lang-black-text' }); } } handleClick = (language) => { this.handleUpdateLanguage(language); this.handleStyleColorChange(language); } render() { return ( <B onLanguageUpdate={this.handleClick} selectedLanguage={this.state.selectedLanguage} /> ); } } const B = (props) => { const languages = ['English', 'Spanish', 'Japanese', 'Italian']; const languageListFormatted = languages.map(language => { return ( <li key={language} style={props.selectedLanguage === language ? {background: 'yellow'} : {}} onClick={() => props.onLanguageUpdate(language)}>{language} </li> ); }); return ( <ul className="languages">{languageListFormatted}</ul> ); } ReactDOM.render( <A />, document.getElementById('app') ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div> 

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

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