Skip to content

Instantly share code, notes, and snippets.

@jdeniau
Created June 27, 2018 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdeniau/f2d02c285c815345fcfc53752545e8a8 to your computer and use it in GitHub Desktop.
Save jdeniau/f2d02c285c815345fcfc53752545e8a8 to your computer and use it in GitHub Desktop.
keep your "button" instance and change it's props instead of creating new button when props change, this way you keep keyboard navigation available
import React from 'react';
// Don't do this
const Button = ({ isChecked }) => (
<div>
{isChecked && <button className="btn btn--checked">I'm checked</button>}
{!isChecked && <button className="btn btn--unchecked">I'm not checked</button>}
</div>
);
// Do this instead
const Button = ({ isChecked }) => (
<div>
<button className={`btn ${isChecked ? "btn--checked" : "btn--unchecked"}`}>
{isChecked ? "I'm checked" : "I'm not checked"}
</button>
</div>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment