0

I am trying to import a JSON object which is in an external file using React, and display its content inside a loop containing HTML. The goal is to display all that into a JQuery Slider (Slick).

I think I succeeded in importing the JSON, but I can't display it. Most of the results I find on the internet are in ES5 also.

Here is my slider.js file:

import React from 'react';
import slidesData from './slides.json';

export class Slider extends React.Component {
    constructor(){
        super();
        this.state = {
            slides: slidesData,
        }
    }

    render() {
        const list = this.state.slides.map(d => <div className="classTest">ID HERE {slidesData.id} - DESCRIPTION HERE {slidesData.desc}</div>);
        return (
            <div className="carousel">
                <div className="data">
                    <div>list before {list} list after</div>
                </div>
            </div>
        );
    }
}

slides.json:

[
    {
        "id": 1,
        "desc": "text desc 1"
    },
    {
        "id": 2,
        "desc": "text desc 2"
    },
    {
        "id": 3,
        "desc": "text desc 3"
    },
    {
        "id": 4,
        "desc": "text desc 4"
    }
]

Result in the browser:

list before
ID HERE - DESCRIPTION HERE
ID HERE - DESCRIPTION HERE
ID HERE - DESCRIPTION HERE
ID HERE - DESCRIPTION HERE
list after

The files are in the same folder. So the {list} is displayed 4 times (like the items it contains) but it doesn't display the content. What am I missing?

And then, I would like to repeat each <div className="data"> with a for loop displaying every time the JSON has content but until I find why the json is not getting displayed properly I don't want to try more complicated code.

Thanks =)

2
  • 2
    this.state.slides.map(d => <div className="classTest">ID HERE {d.id} - DESCRIPTION HERE {d.desc}</div>); Commented Aug 29, 2017 at 7:56
  • 1
    There is React port of Slick slider, you can use that. (it's better not to use jQuery in your react app). Have a look at this github.com/akiran/react-slick Commented Aug 29, 2017 at 8:09

2 Answers 2

2

You are accessing the wrong object in your .map(). You should access d instead of slidesData:

 render() {
        const list = this.state.slides.map((d, index) => <div className="classTest" key={index}>ID HERE {d.id} - DESCRIPTION HERE {d.desc}</div>); //changed
        return (
            <div className="carousel">
                <div className="data">
                    <div>list before {list} list after</div>
                </div>
            </div>
        );
    }

EDIT

You should also provide a key to your tags within the .map(). I edited my original answer.

Sign up to request clarification or add additional context in comments.

9 Comments

This works! I thought I did that and it didn't work... Well, getting used to webpack server refresh. If I want to learn how to iterate a loop on the <div className="data">, should I ask it here or in a new topic? Thanks!
Yes, ask another question for that
It's going to be hard. I got -1 on this question and now I'm banned "You have reached your question limit". I really tried my best but I still can't get how stackoverflow is working. I try quoting, putting into code, using proper words, questions, looking into hundreds of topics for weeks before asking... I'm depressed now. Never happened to me before on the whole internet for 15 years now.
I can see your frustration. If you really need help, let me know (discord, skype, ...)
Yes, I think I need help. I don't see any way to get out of this situation, by editing or whatever. And I really don't understand my ban. Can I contact you privately?
|
1

From the looks of it your map is referencing 'd', but you're trying to access via {slidesData.id}

try:

const list = this.state.slides.map(d => return (<div 
className="classTest">ID HERE `${d.id}` - DESCRIPTION HERE `${d.desc}`</div>));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.