0

Lets say there are 2 components Main and Card, Card is child of Main in React.js. Card will pass in an string value to Main. Thus state in Main for the Card would update.

var Card= React.createClass({
...
this.props.updateName(loginVal.value); //pass in a 'string' to updateName()
...
});

var Main = React.createClass({
    getInitialState: function() {
        return {
            names: []
        };
    },
    updateName: function (loginVal) {
        this.setState({ 
            names: this.state.names.concat(loginVal) // I don't understand why using 'concat' works but not 'push'.
        });
    },
    render: function(){
        var cardArr = this.state.names.map(function(item){
            return ( <Card login={item} /> );
        });
        return (
            <div>
                <Form updateName={this.updateName} />
                {cardArr}
            </div>
        );
    }
});

My question is why this.state.names.concat(loginVal) works when this.state.names is an empty array and loginVal is a string. [].concat('string') just don't make sense to me.

2 Answers 2

1

push adds the provided element(s) and returns the new length of the array, so this.state.names would be a number.

concat adds the provided element(s) to a new array and returns it, so this.state.names would be an array with the new name added.

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

6 Comments

I still don't understand. Would you explain it in a different way? I understand push updates original array and concat returns a new array. but I don't understand is why [].concat('string') here? I know my logic must be wrong somewhere.
I think it's very understandable what he wrote. You should write: var names = this.state.names; names.push(loginVal); this.setState({ names: names });
@user2734550 Whoever wrote the code probably just did it to be shorter. Both this.state.names.push(loginVal); this.setState({names: this.state.names}); and this.setState({names: this.state.names.concat(loginVal)}); do the same thing, one is just shorter than the other.
A big fundamental mistake... forgot push returns length. Thanks everyone.
this.state.names.push(loginVal) has the downside of mutating the array. In React, using mutative methods like push is discouraged as it makes it impossible to compare two snapshots of your component's props and state. See shouldComponentUpdate for example.
|
1

[].concat(["string", "other string") will push each value to an new empty array. You can also pass a simple string instead of an array because the method was overwritten to make it more flexible.

push("string") do not work because it will return the new length of the array. But it will change the array instead of creating a new one.

you could use this:

this.state.names.push(value);
this.setState({
    names: this.state.names
});

3 Comments

Are you saying if it is NOT empty array, it would return new array. Otherwise, it doesn't?
Now that make sense. I wonder when they overwritten that, didn't know array can concat string directly
thats the power of JavaScript/functional programming. ;-)

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.