Darty
Darty

Reputation: 3

Foundation Accordion not working in React

I am using foundation accordion in my react component.

So this code works if get the component to render static HTML however, if I do it via loop the accordions are not clickable.

UL React code:

return (
<ul className="accordion" data-accordion data-allow-all-closed>
    this.state.test.map((data,index) => {
     return (<LI id={data.ID} value={data.Intro} />)
   }
</ul>
)

LI code

return (
            <li className="accordion-item" data-accordion-item  key={this.props.id}>
                <a href="#" className="accordion-title">{this.props.value}</a>
                <div className="accordion-content" data-tab-content>
                    <strong>{this.props.value}</strong>
                    <p>Test</p>
                </div>
            </li>
        )

ComponentDidMount

new Accordion($(".accordion"), {
            slideSpeed: 500,
            multiExpand: true
        });
   fetch("URL")
            .then(res => res.json())
            .then(
                (result) => {
                    this.setState ({
                        isLoaded: true,
                        items : [
                            { ID: 1, Intro: "Label sdf1" },
                            { ID: 2, Intro: "Label 2" },
                            { ID: 3, Intro: "Label 3" }
                        ]
                    })
                },
                (error) => {
                    this.setState({
                        isLoaded: true,
                        error
                    });
                }
            )

Contructor

constructor(props){

     super(props);
       this.state = {
           alerts:[],
           items:[],

       }

 }

I have edited @Pavelcode here to replicate my issue

Upvotes: 0

Views: 604

Answers (1)

Pavel Denisjuk
Pavel Denisjuk

Reputation: 1473

Your LI component has broken markup, the <a> does not have a closing tag.

enter image description here

Should be <a href="#" className="accordion-title">{this.props.value}</a>.

EDIT:

The solution is in this sandbox: https://codesandbox.io/s/m76m5376ly?fontsize=14

Note that when using with React, Foundation must be initialized in one of the many ways described in the docs. In this example I used the componentDidMount to initialize the accordion.

Upvotes: 1

Related Questions