React 16: TypeError: this.state.userInput.map is not a function












1















I have simple react app, which a user can enter a text input , I want to display character entered as list



here is what I have done so far:



    app components 
import React, { Component } from 'react';
import './App.css';
import Char from './Char/Char'


class App extends Component {
state ={
userInput: ''
}

handleChange =(e)=>{
this.setState({
userInput: e.target.value
})
render() {

const charList = this.state.userInput.map(char =>{
return (
<Char character={char} />
)
})

return (
<div className="container">
<div className="App card">
<input type="text" placeholder="enter a text"
value={this.state.userInput} onChange={this.handleChange}></input>
<p>Paragraph: {this.state.userInput}</p>

{charList}

</div>
</div>
);
}
}

export default App;


Here is Char component



import React from 'react'

const Char =(props) =>{

const divStyle = {
display: 'inline-block',
margin: '16px',
border: '1px solid pink',
padding: '16px',
textAlign: 'center',
backgroundColor: 'orange'

};
return (
<div style={divStyle}>
<p>{props.character}</p>
</div>
);
}

export default Char;


so when I run the app I get the following error




TypeError: this.state.userInput.map is not a function




What do I need to change to get rid of this distubing litle error?










share|improve this question























  • Evan I know that but I am trying to find a way to make this work , I am sure there is a way

    – user9964622
    Nov 21 '18 at 1:36






  • 1





    Try "this.state.userInput.split("").map(" instead

    – Caleb H.
    Nov 21 '18 at 1:37











  • If you knew that, why are you calling map on a string? Surely the question is "how do I loop each character in a string" or something. How is react even relevant?

    – Evan Trimboli
    Nov 21 '18 at 1:37


















1















I have simple react app, which a user can enter a text input , I want to display character entered as list



here is what I have done so far:



    app components 
import React, { Component } from 'react';
import './App.css';
import Char from './Char/Char'


class App extends Component {
state ={
userInput: ''
}

handleChange =(e)=>{
this.setState({
userInput: e.target.value
})
render() {

const charList = this.state.userInput.map(char =>{
return (
<Char character={char} />
)
})

return (
<div className="container">
<div className="App card">
<input type="text" placeholder="enter a text"
value={this.state.userInput} onChange={this.handleChange}></input>
<p>Paragraph: {this.state.userInput}</p>

{charList}

</div>
</div>
);
}
}

export default App;


Here is Char component



import React from 'react'

const Char =(props) =>{

const divStyle = {
display: 'inline-block',
margin: '16px',
border: '1px solid pink',
padding: '16px',
textAlign: 'center',
backgroundColor: 'orange'

};
return (
<div style={divStyle}>
<p>{props.character}</p>
</div>
);
}

export default Char;


so when I run the app I get the following error




TypeError: this.state.userInput.map is not a function




What do I need to change to get rid of this distubing litle error?










share|improve this question























  • Evan I know that but I am trying to find a way to make this work , I am sure there is a way

    – user9964622
    Nov 21 '18 at 1:36






  • 1





    Try "this.state.userInput.split("").map(" instead

    – Caleb H.
    Nov 21 '18 at 1:37











  • If you knew that, why are you calling map on a string? Surely the question is "how do I loop each character in a string" or something. How is react even relevant?

    – Evan Trimboli
    Nov 21 '18 at 1:37
















1












1








1








I have simple react app, which a user can enter a text input , I want to display character entered as list



here is what I have done so far:



    app components 
import React, { Component } from 'react';
import './App.css';
import Char from './Char/Char'


class App extends Component {
state ={
userInput: ''
}

handleChange =(e)=>{
this.setState({
userInput: e.target.value
})
render() {

const charList = this.state.userInput.map(char =>{
return (
<Char character={char} />
)
})

return (
<div className="container">
<div className="App card">
<input type="text" placeholder="enter a text"
value={this.state.userInput} onChange={this.handleChange}></input>
<p>Paragraph: {this.state.userInput}</p>

{charList}

</div>
</div>
);
}
}

export default App;


Here is Char component



import React from 'react'

const Char =(props) =>{

const divStyle = {
display: 'inline-block',
margin: '16px',
border: '1px solid pink',
padding: '16px',
textAlign: 'center',
backgroundColor: 'orange'

};
return (
<div style={divStyle}>
<p>{props.character}</p>
</div>
);
}

export default Char;


so when I run the app I get the following error




TypeError: this.state.userInput.map is not a function




What do I need to change to get rid of this distubing litle error?










share|improve this question














I have simple react app, which a user can enter a text input , I want to display character entered as list



here is what I have done so far:



    app components 
import React, { Component } from 'react';
import './App.css';
import Char from './Char/Char'


class App extends Component {
state ={
userInput: ''
}

handleChange =(e)=>{
this.setState({
userInput: e.target.value
})
render() {

const charList = this.state.userInput.map(char =>{
return (
<Char character={char} />
)
})

return (
<div className="container">
<div className="App card">
<input type="text" placeholder="enter a text"
value={this.state.userInput} onChange={this.handleChange}></input>
<p>Paragraph: {this.state.userInput}</p>

{charList}

</div>
</div>
);
}
}

export default App;


Here is Char component



import React from 'react'

const Char =(props) =>{

const divStyle = {
display: 'inline-block',
margin: '16px',
border: '1px solid pink',
padding: '16px',
textAlign: 'center',
backgroundColor: 'orange'

};
return (
<div style={divStyle}>
<p>{props.character}</p>
</div>
);
}

export default Char;


so when I run the app I get the following error




TypeError: this.state.userInput.map is not a function




What do I need to change to get rid of this distubing litle error?







javascript reactjs typescript react-redux






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 1:30









user9964622user9964622

1,14311037




1,14311037













  • Evan I know that but I am trying to find a way to make this work , I am sure there is a way

    – user9964622
    Nov 21 '18 at 1:36






  • 1





    Try "this.state.userInput.split("").map(" instead

    – Caleb H.
    Nov 21 '18 at 1:37











  • If you knew that, why are you calling map on a string? Surely the question is "how do I loop each character in a string" or something. How is react even relevant?

    – Evan Trimboli
    Nov 21 '18 at 1:37





















  • Evan I know that but I am trying to find a way to make this work , I am sure there is a way

    – user9964622
    Nov 21 '18 at 1:36






  • 1





    Try "this.state.userInput.split("").map(" instead

    – Caleb H.
    Nov 21 '18 at 1:37











  • If you knew that, why are you calling map on a string? Surely the question is "how do I loop each character in a string" or something. How is react even relevant?

    – Evan Trimboli
    Nov 21 '18 at 1:37



















Evan I know that but I am trying to find a way to make this work , I am sure there is a way

– user9964622
Nov 21 '18 at 1:36





Evan I know that but I am trying to find a way to make this work , I am sure there is a way

– user9964622
Nov 21 '18 at 1:36




1




1





Try "this.state.userInput.split("").map(" instead

– Caleb H.
Nov 21 '18 at 1:37





Try "this.state.userInput.split("").map(" instead

– Caleb H.
Nov 21 '18 at 1:37













If you knew that, why are you calling map on a string? Surely the question is "how do I loop each character in a string" or something. How is react even relevant?

– Evan Trimboli
Nov 21 '18 at 1:37







If you knew that, why are you calling map on a string? Surely the question is "how do I loop each character in a string" or something. How is react even relevant?

– Evan Trimboli
Nov 21 '18 at 1:37














1 Answer
1






active

oldest

votes


















3














There is no map method for a string.



You can make it an array easily enough:



this.state.userInput.split('').map(c => <Char character={c} />);






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404077%2freact-16-typeerror-this-state-userinput-map-is-not-a-function%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    There is no map method for a string.



    You can make it an array easily enough:



    this.state.userInput.split('').map(c => <Char character={c} />);






    share|improve this answer




























      3














      There is no map method for a string.



      You can make it an array easily enough:



      this.state.userInput.split('').map(c => <Char character={c} />);






      share|improve this answer


























        3












        3








        3







        There is no map method for a string.



        You can make it an array easily enough:



        this.state.userInput.split('').map(c => <Char character={c} />);






        share|improve this answer













        There is no map method for a string.



        You can make it an array easily enough:



        this.state.userInput.split('').map(c => <Char character={c} />);







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 1:37









        Evan TrimboliEvan Trimboli

        26.4k43453




        26.4k43453






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404077%2freact-16-typeerror-this-state-userinput-map-is-not-a-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith