Search code examples
gitreactjsfluxreactjs-flux

React-tagsinput module implementation in React Flux project; there is an error in my code for the tag functionality?


I've tried a few different implementations (you can see them commented out).

Once I launch the project, the screen displays an input text box with 'tag1 tag2' written in it- because that's what I intialized them as; but I can't do anything as whenever I try to manipulate them (delete/spacebar/add another letter) they get replaced by

[object Object]

in the text field and then I can't do anything still, except going back over it with arrowkeys; but if I try anything else, it'll push the cursor back to front.


I've tried a few variations like:
TagsCom.React.createElement("text", null,...) or just TagsCom.createElement("text", null,...)
or even just replacing the TagsCom with TagsInput but the only line of code that even remotely works so far is:

<TagsInput value={this.state.tags} onChange={this.handleChange} />

And though this has solved the object problem, it's still all static text and not acting like tags

Here is my updated component code:

var React      = require('react');
var ReactDOM   = require('react-dom');
var TagsInput1 = require('react-tagsinput');
var TagsCom    = require('./react-tagsinput.js');
//var TagsCSS  = require('react-tagsinput/react-tagsinput.css');

var TagsComponent = React.createClass
({
    displayName    : "TagsComponent",
    getInitialState: function()
    {
        return {
            tags: ["tag1", "tag2"]
        };
    },

    saveTags: function (){
        console.log("tags: ", this.state.tags.join(", "));
    },

    handleChange: function(event){
    this.setState({tags: event.target.value});
    },

    render: function ()
    {

      <TagsInput value={this.state.tags} onChange={this.handleChange} />

    }
});
module.exports = TagsComponent;

and here is the git link to the react-tagsinput module that has all the other files https://github.com/olahol/react-tagsinput


Solution

  • After much trial and error, I've figured out the correct render function- it renames TagsInput to ReactTagsInput and leaves the onChange parameter as 'value':

    var React = require('react');
    var ReactDOM = require('react-dom');
    var ReactTagsInput = require('./react-tagsinput.js');
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react-with-addons.js"></script>
    
    tags:[];  
    var TagsComponent = React.createClass({
        displayName: "TagsComponent",
    
        getInitialState: function() {
            return {
                tags: ["tag1", "tag2"]
            };
        },
    
        saveTags: function () {
            console.log("tags: ", this.state.tags.join(", "));
        },
    
        handleChange: function(value){
            this.setState({tags: value});
        },
    
        render: function () {
            return 
    
        <div>
           <ReactTagsInput value={this.state.tags} onChange={this.handleChange}/>   
         </div>;
        }
    
        });
    
    
     module.exports = TagsComponent;