简体   繁体   English

反应就像地图的按钮。 json资料

[英]react like button for map. json data

yes hello, 是的你好,

how do I make a like button for something like this? 我该如何为like button做点赞like button

http://jsfiddle.net/2nq3joy6/ http://jsfiddle.net/2nq3joy6/

i thought id be able to do it by map. 我以为id可以通过地图做到。 to {this.state.count} ie. {this.state.count} like {l.this.state.count} in this case as like the other items, but no. {l.this.state.count}在这种情况下,就像其他项目一样,但是没有。

/** @jsx React.DOM */

// Let's create a "real-time search" component

var SearchExample = React.createClass({

    getInitialState: function(){
        return { 
            searchString: '',
            count: 0
        };
    },

    incrementCount: function() {
        this.setState({
            count: this.state.count + 1
        });
    },


    handleChange: function(e){
        // With setState the current and previous states are merged.
        this.setState({
            searchString:e.target.value
        });
    },

    render: function() {

        var libraries = this.props.items,
        searchString = this.state.searchString.trim().toLowerCase();

        if(searchString.length > 0){
            console.log('searching');
            // We are searching. Filter the results.

            libraries = libraries.filter(function(l){
                return l.name.toLowerCase().match( searchString );
            });
        }

        return <div>
        <input type="text" className="form-control" value={this.state.searchString} onChange={this.handleChange} placeholder="Type here" />

        <div className="row"> 

        { libraries.map(function(l){
            return <div className="col-xs-4 text-center">

            <h5>{l.name} </h5> 
            <img src="http://placehold.it/350x150" className="img-responsive" />
            <a href={l.url}>{l.url}</a> 
            <p>likes x</p>
            <button className="btn btn-primary" onClick={this.incrementCount}>like</button> 

            </div>
        })}

        </div>

        </div>;

    } // render end
});


   var libraries = [

   { name: 'Backbone.js', likes: 3, comments: 5, url: 'http://documentcloud.github.io/backbone/'},
   { name: 'AngularJS', likes: 3, comments: 5, url: 'https://angularjs.org/'},
   { name: 'jQuery', likes: 3, comments: 5, url: 'http://jquery.com/'},
   { name: 'Prototype', likes: 3, comments: 5, url: 'http://www.prototypejs.org/'},
   { name: 'React', likes: 3, comments: 5, url: 'http://facebook.github.io/react/'},
   { name: 'Ember', likes: 3, comments: 5, url: 'http://emberjs.com/'},
   { name: 'Knockout.js', likes: 3, comments: 5,  url: 'http://knockoutjs.com/'},
   { name: 'Dojo', likes: 3, comments: 5, url: 'http://dojotoolkit.org/'},
   { name: 'Mootools', likes: 3, comments: 5, url: 'http://mootools.net/'},
   { name: 'Underscore', likes: 3, comments: 5, url: 'http://documentcloud.github.io/underscore/'},
   { name: 'Lodash', likes: 3, comments: 5, url: 'http://lodash.com/'},
   { name: 'Moment', likes: 3, comments: 5, url: 'http://momentjs.com/'},
   { name: 'Express', likes: 3, comments: 5, url: 'http://expressjs.com/'},
   { name: 'Koa', likes: 3, comments: 5, url: 'http://koajs.com/'},

   ];

// Render the SearchExample component on the page

React.renderComponent(
    <SearchExample items={ libraries } />,
    document.getElementById('content')
    );

There are a few ways to implement what you want. 有几种方法可以实现您想要的。 In the following example I chose to increment the like count on the original libraries variable and forceUpdate afterwards. 在下面的示例中,我选择增加原始libraries变量的like计数,然后再forceUpdate If you were following flux architecture, you would probably do that with an increment action that would update LibraryStore and then cause a change event. 如果您遵循的是磁通量体系结构,则可能需要执行一个增量操作来执行此操作,该操作将更新LibraryStore,然后导致更改事件。

You could also chose to create a new component for library items and hold like count in like property of its state as the commenters say. 您还可以选择为library项目创建一个新组件,并像评论者所说的like ,将其状态保持在like属性中。

The way to go probably depends on what will you do next with the incremented like count you have. 前进的方式可能取决于您将要增加的计数(计数)接下来要做什么。

Having said that here is a working example where count is incremented on each button click: 前面已经说过,这是一个工作示例,其中每次单击按钮时计数都会增加:

jsFiddle : http://jsfiddle.net/bftxz5n1/ jsFiddle: http : //jsfiddle.net/bftxz5n1/

/** @jsx React.DOM */

// Let's create a "real-time search" component

var SearchExample = React.createClass({

    getInitialState: function(){
        return { 
            searchString: '',
            count: 0
        };
    },

    incrementCount: function(l) {
        l.likes = l.likes + 1;
        this.forceUpdate();
    },


    handleChange: function(e){
        // With setState the current and previous states are merged.
        this.setState({
            searchString:e.target.value
        });
    },

    render: function() {

        var libraries = this.props.items,
        searchString = this.state.searchString.trim().toLowerCase();

        if(searchString.length > 0){
            console.log('searching');
            // We are searching. Filter the results.

            libraries = libraries.filter(function(l){
                return l.name.toLowerCase().match( searchString );
            });
        }

        return <div>
                    <input type="text" className="form-control" value={this.state.searchString} onChange={this.handleChange} placeholder="Type here" />

                    <div className="row"> 

                        { libraries.map(function(l){
                            return <div className="col-xs-4 text-center">

                                <h5>{l.name} </h5> 
                                <img src="http://placehold.it/350x150" className="img-responsive" />
                                <a href={l.url}>{l.url}</a> 
                                <p>likes {l.likes}</p>
                                <button className="btn btn-primary" onClick={this.incrementCount.bind(this,l)}>like</button> 

                            </div>
                        }.bind(this))}

                    </div>

                </div>;

    } // render end
});


var libraries = [

    { name: 'Backbone.js', likes: 3, comments: 5, url: 'http://documentcloud.github.io/backbone/'},
    { name: 'AngularJS', likes: 3, comments: 5, url: 'https://angularjs.org/'},
    { name: 'jQuery', likes: 3, comments: 5, url: 'http://jquery.com/'},
    { name: 'Prototype', likes: 3, comments: 5, url: 'http://www.prototypejs.org/'},
    { name: 'React', likes: 3, comments: 5, url: 'http://facebook.github.io/react/'},
    { name: 'Ember', likes: 3, comments: 5, url: 'http://emberjs.com/'},
    { name: 'Knockout.js', likes: 3, comments: 5,  url: 'http://knockoutjs.com/'},
    { name: 'Dojo', likes: 3, comments: 5, url: 'http://dojotoolkit.org/'},
    { name: 'Mootools', likes: 3, comments: 5, url: 'http://mootools.net/'},
    { name: 'Underscore', likes: 3, comments: 5, url: 'http://documentcloud.github.io/underscore/'},
    { name: 'Lodash', likes: 3, comments: 5, url: 'http://lodash.com/'},
    { name: 'Moment', likes: 3, comments: 5, url: 'http://momentjs.com/'},
    { name: 'Express', likes: 3, comments: 5, url: 'http://expressjs.com/'},
    { name: 'Koa', likes: 3, comments: 5, url: 'http://koajs.com/'},

];

// Render the SearchExample component on the page

React.renderComponent(
    <SearchExample items={ libraries } />,
    document.getElementById('content')
);

Is that what you mean? 是你的意思吗?

In case I'm right, you have to move the count to the library itself, ideally it should be another component, but to avoid changing your code too much I just bound the index of the library being liked to the incrementCount method and used it to retrieve the library receiving the like. 如果我是对的,您必须将计数移到库本身,理想情况下它应该是另一个组件,但是为了避免过多更改代码,我只将喜欢的库的索引绑定到了incrementCount方法并使用了它检索接收类似的库。

BTW, the search is still broken, if libraries is mutable, it should be in the state, otherwise there is no way retrieve the right library on incrementCount method. 顺便说一句,搜索仍然中断,如果库是可变的,则它应该处于状态,否则无法通过incrementCount方法检索正确的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM