0

Using React, I am implementing Dexie.js for this example. However, I don't believe this to be particularly important. I would like to know how to execute a loop of the objects in my IndexDB database using React.

As shown in my code below, dayta holds my database, and stores friends with name and age values. When my function carveit is run, it takes what users have typed in and places that value on {this.state.etv}. The code works.

However, I do not know how to have {this.state.etv} show ALL entries. As is, it only shows the most recent addition. I understand I would have to execute some kind of loop, and use the map function, but I am unsure how to go about that.

var React = require('react');

module.exports = React.createClass({
    getInitialState: function(){
        return { etv:'' }    
    },

    carveit:function(){
        var enteringtitle = document.getElementById('entertitle').value;
        var enteringmessage = document.getElementById('enterentry').value;    


        var dayta = new Dexie('testcase');
        dayta.version(1).stores({
            friends:'name, age'    
        });   


        dayta.open().catch(function(error){
        alert("Oh no son: " +error);    
        });

        dayta.friends.add({
            name: enteringtitle,
            age: enteringmessage    
        });    

        dayta.friends.each((friend)=>{
            this.setState({etv: friend.name}); 
        });    

    },    

    functionrun:function(){
        return (<div>
            <ul>
                <li>{this.state.etv}</li>   
            </ul>        
            <p>Entry Title</p>    
            <input id="entertitle" type="text" className="form-control"/>

            <p>Your Entry</p>    
            <textarea id="enterentry" className="form-control"></textarea>


            <input id="entrytitle" type="submit" value="Publish" onClick={this.carveit} />        
        </div>);
    },    

    render:function(){
        if (this.props.contentadd){
            return this.functionrun();
        }
        else {
            return null;
        }    
    }

});

1 Answer 1

2

You can perform the loop separately and have it return jsx. You can then use the return value in your template since arrays of jsx partials are supported.

render: function() {
    var itemElements = this.state.items.map(function(item, i) {
        return (
            <li>{item}</li>
        );
    });

    return (
        <ul>
            {itemElements}
        </ul>
    );
}
4
  • Hey. I get this.state.etv.map is not a function error when following your structure.
    – Jason Chen
    Commented Aug 1, 2016 at 19:58
  • 1
    Right, if this.state.etv isn't an array, then it won't have a map() function. Your state variable needs to be an array in order to loop over it. I'll make my example more generic so that isn't misleading.
    – Soviut
    Commented Aug 1, 2016 at 20:01
  • So I added a getInitialState of etv:[{name: ' ', age: ' ' }] so that it is an array. But I still get this.state.etv.map is not a function. In addition, I added var itemElements = this.state.etv.map(function(item, i){ return (li key={i}>{item.name}</li);});
    – Jason Chen
    Commented Aug 1, 2016 at 21:18
  • 1
    You should console.log(this.state.etv) and see what value it has and console.log(typeof this.state.etv) to see what type it is. For all you know your state may not be set up correctly.
    – Soviut
    Commented Aug 2, 2016 at 1:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.