Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed unnecessary component state, used props instead #20791

Merged
merged 3 commits into from
Jan 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions docs/blog/2017-10-17-building-i18n-with-gatsby/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,24 +231,18 @@ import { translate } from "react-i18next"
class LanguageSwitcher extends Component {
constructor(props) {
super(props)
const { i18n } = this.props
this.state = { language: i18n.language }

this.handleChangeLanguage = this.handleChangeLanguage.bind(this)
}

componentWillReceiveProps(nextProps) {
this.setState({ language: nextProps.i18n.language })
}

handleChangeLanguage(lng) {
const { i18n } = this.props
i18n.changeLanguage(lng)
}

renderLanguageChoice({ code, label }) {
const buttonClass = classNames("LanguageSwitcher__button", {
"LanguageSwitcher__button--selected": this.state.language === code,
"LanguageSwitcher__button--selected": this.props.i18n.language === code,
})

return (
Expand Down Expand Up @@ -279,8 +273,8 @@ class LanguageSwitcher extends Component {
export default translate("LanguageSwitcher")(LanguageSwitcher)
```

This is a pretty simple component. We're setting the `language` state based on
the i18n prop so that we can check which language is currently active and show
This is a pretty small component. We're getting the `language` in the
i18n prop so that we can check which language is currently active and show
that in our menu.

The `handleLanguageChange` function just wraps the `react-i18n` function passed
Expand Down