简体   繁体   中英

How to check state inside styled-component?

Instead of styling the Li component via inline style, I want to do it via styled-component. How can I check the state and then assign the color red if the current Li is the selected language?

const Li = styled.li`
  border: 0;

  //Set color to red if this component is selected
  ${this.state => this.state.selectedLanguage`
    color: 'red';
  `}

`;

class Popular extends Component {
    constructor(props) {
        super(props);
        this.state = {
            selectedLanguage: 'All'
        }

        this.updateLanguage = this.updateLanguage.bind(this);
    }

    updateLanguage(lang) {
        this.setState(function() {
            return {
                selectedLanguage: lang
            };
        });
    }
    render() {
        const languages = ['All', 'Javascript', 'Java', 'Go', 'Rust'];
        return (
            <ul className={`list-group d-flex flex-row justify-content-center ${this.props.className}`}>
                {languages.map(function(lang){
                    return (
                        <Li
                            key={lang}
                            // style={lang === this.state.selectedLanguage ? {color: '#d00d1b'} : null}
                            onClick={this.updateLanguage.bind(null, lang)}
                            className={`list-group-item p-2 ${this.props.className}`}>
                            {lang}
                        </Li>
                    )
                }, this)}
            </ul>
        );
    }
}

export default Popular;

code is based from tyler mcginnis - react fundamentals

Passing down isSelected as props then calling the css helper if true

const selectedLanguageStyle = css`
  color: '#d00d1b'
`

const Li = styled.li`
  border: 0;
  ${(props) => props.isSelected && selectedLanguageStyle}
`;

class Popular extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedLanguage: 'All'
    }

    this.updateLanguage = this.updateLanguage.bind(this);
  }

  updateLanguage(lang) {
    this.setState(function() {
      return {
        selectedLanguage: lang
      };
    });
  }
  render() {
    const languages = ['All', 'Javascript', 'Java', 'Go', 'Rust'];
    return (
      <ul className={`list-group d-flex flex-row justify-content-center ${this.props.className}`}>
        {languages.map(function(lang){
          return (
            <Li
              key={lang}
              isSelected={lang === this.state.selectedLanguage}
              onClick={this.updateLanguage.bind(null, lang)}
              className={`list-group-item p-2 ${this.props.className}`}>
              {lang}
            </Li>
          )
        }, this)}
      </ul>
    );
  }
}

export default Popular;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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