@@ -19,38 +19,30 @@ export default class ComponentSidebarSection extends PureComponent {
1919 onTitleClick : PropTypes . func ,
2020 }
2121
22- constructor ( props ) {
23- super ( props )
24- this . state = {
25- isActiveByProps : this . isActiveAccordion ( ) ,
26- }
27- }
22+ state = { }
2823
29- componentWillReceiveProps ( nextProps ) {
30- const isActiveByProps = this . isActiveAccordion ( nextProps )
31- const didCloseByProps = this . state . isActiveByProps && ! isActiveByProps
24+ static getDerivedStateFromProps ( props , state ) {
25+ const isActiveByProps = ( props . examples || [ ] ) . some ( ( item ) => {
26+ const exampleHash = examplePathToHash ( item . examplePath )
27+ return exampleHash === props . activePath
28+ } )
29+ const didCloseByProps = state . isActiveByProps && ! isActiveByProps
3230
3331 // We allow the user to open accordions, but we close them when we scroll passed them
34- this . setState ( prevState => ( {
32+ return {
3533 isActiveByProps,
36- isActiveByUser : didCloseByProps ? false : prevState . isActiveByUser ,
37- } ) )
34+ isActiveByUser : didCloseByProps ? false : state . isActiveByUser ,
35+ }
3836 }
3937
40- handleItemClick = examplePath => ( e ) => {
38+ handleItemClick = ( examplePath ) => ( e ) => {
4139 _ . invoke ( this . props , 'onItemClick' , e , { examplePath } )
4240 }
4341
4442 handleTitleClick = ( ) => {
45- this . setState ( prevState => ( { isActiveByUser : ! prevState . isActiveByUser } ) )
43+ this . setState ( ( prevState ) => ( { isActiveByUser : ! prevState . isActiveByUser } ) )
4644 }
4745
48- isActiveAccordion = ( props = this . props ) =>
49- ( props . examples || [ ] ) . findIndex ( ( item ) => {
50- const exampleHash = examplePathToHash ( item . examplePath )
51- return exampleHash === props . activePath
52- } ) !== - 1
53-
5446 render ( ) {
5547 const { activePath, examples, sectionName } = this . props
5648 const { isActiveByProps, isActiveByUser } = this . state
0 commit comments