0

I am trying to map an array of strings that contains img URL

but from checking logs on the way i found out that when 'pictures' array arrive to the .map,

it wont work even when i tried to put there console.log('test) seems it wont enter the function.

    const ImageList = ({pictures}) => {
        console.log(pictures)
        let picture;
        picture = pictures.map((pic) => {
            console.log(pic);
            return ( <Col sm={6} md={4}>
                    <ListGroupItem><img src={pic} alt="test"/></ListGroupItem>
                </Col>
            )
        });
        console.log(picture);
        return (
            <ListGroup>
                {picture}
            </ListGroup>
        )
    };

    export default ImageList;

result from console:

enter image description here

update: this is the code, @Bergi was right, when i did

the array is empty, so basically it goes into the Image List empty, how can i make it synchronize before it goes in?

  console.log(JSON.stringify(pictures));

this is the code:

    class App extends Component {

        constructor(props){
            super(props);

            this.state = {
                picturesArr: [],
                urlArr: [],
                titleArr:[],
                nextPage:'',
                prevuisPage:''
            };

            this.loadSubreddit = this.loadSubreddit.bind(this);
        }

        loadSubreddit(subre){
            let pictures = [];
            let urls =[];
            let titles=[];
            let nextpage ='';
            let pageback='';
            reddit.hot(subre).limit(5).fetch(function(res) {
                console.log(res);
                nextpage = res.data.after.toString();
                // pageback = res.data.before.valueOf();
                for(let i =0; i<res.data.children.length; i++){
                    pictures.push(res.data.children[i].data.url);
                    urls.push(res.data.children[i].data.permalink);
                    titles.push(res.data.children[i].data.title);
                }});

            this.setState({
                picturesArr: pictures,
                urlArr: urls,
                titleArr: titles,
                nextPage: nextpage,
                prevuisPage: pageback
            }, () => {
                console.log(this.state)
            });
            console.log(pictures);
            console.log(JSON.stringify(pictures));

            }


        componentWillMount() {
            console.log('comp is mounting');
            this.loadSubreddit('cats');
        }

      render() {
            return (
            <div className="App">
            <div className="App-header">
              <img src={logo} className="App-logo" alt="logo" />
              <h2>Welcome to React</h2>
            </div>
                <Col sm={8} md={10} smOffset={2} mdOffset={1} >
                    <ImageList picturese={this.state.picturesArr} />
                </Col>
            </div>
        );
      }
    }

    export default App;
Ben Porat
  • 583
  • 1
  • 4
  • 6
  • 4
    The array *is* empty when the `map` is called upon it. Check what the `i` icon has to tell. – Bergi May 03 '17 at 12:44
  • But you *do* get a correct render, right? – Chris May 03 '17 at 12:47
  • When i "console.log(pic);" it wont even show up and when i "console.log(picture);" , the empty array it is after the map function finished. i don't get a correct render – Ben Porat May 03 '17 at 12:50
  • @Bergi, in the link you referenced, first, the bug was fixed and second, it is about console moving to the end of the stack. here in order to get empty array, the console.log(picture) should have move to the start of the function. in addition he says that he doesn't get the correct result which makes it weird. Ben Porat - could u provide fiddle? – jony89 May 03 '17 at 12:56
  • 1
    I've created a simulation of your code (without the components `Col`, `ListGroup` and `ListGroupItem`), and everything seems to be fine. https://jsbin.com/mihizufawe/edit?js,console,output – Julio Betta May 03 '17 at 13:02
  • wow, @juliobetta that is really weird. Give me couple of minutes i will try a different IDE – Ben Porat May 03 '17 at 13:09
  • @jony89 Well the bug *is* fixed, notice that it logs `[]` and that info icon. The log statement that it applies to is `console.log(pictures)` which is in the start of the function, not `console.log(picture)` (which is obviously empty). And yes, he doesn't get the expected result because the `pictures` array is filled asynchronously. – Bergi May 03 '17 at 13:09
  • it's working please check http://plnkr.co/edit/0maYMQCc918YpbA1NfGr – Dhaval Patel May 03 '17 at 13:14
  • @Bergi, `pictures` is a function param and has it's data from the start. as it shown also in console. – jony89 May 03 '17 at 13:14
  • 3
    @jony89 `pictures` is empty, as it is shown in the console. It acquires the data only later - that's the whole point. Please re-read the duplicate. – Bergi May 03 '17 at 13:20
  • @Bergi was right all along, thanks for noticing, i edit the queastion so it can reflect better. – Ben Porat May 03 '17 at 14:07
  • Typo here: It should be pictures, not picturese. – Dat Tran May 04 '17 at 03:50

0 Answers0