Dynamically add a span tag to the first word of a string





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















i'm working on a react project where i'm passing titles contained in states to a children element (see bottom).
How can i dynamically add to the first word (and only the first word cause my titles can contain 2 or 3 words) inside the this.state.title inside the child element?



//Parent

import React, { Component } from 'react';
import Child from './child';

class Parent extends Component {

state = {
title:'Admin Picture'
}

render() {
return (
<React.Fragment>
<Child title={this.state.title}>
</React.Fragment>
);
}
}
export default Parent;


//Child

import React, { Component } from 'react';

class Child extends Component {
render(){
return (
<div>
<span className="title">{this.props.title}</span>
</div>
);
}
};
export default Child;









share|improve this question

























  • Where you have the curly braces - {this.state.title} - you can put any Javascript expression. If you are not familiar with splitting strings look at this documentation: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – Mixolydian
    Nov 26 '18 at 21:43


















1















i'm working on a react project where i'm passing titles contained in states to a children element (see bottom).
How can i dynamically add to the first word (and only the first word cause my titles can contain 2 or 3 words) inside the this.state.title inside the child element?



//Parent

import React, { Component } from 'react';
import Child from './child';

class Parent extends Component {

state = {
title:'Admin Picture'
}

render() {
return (
<React.Fragment>
<Child title={this.state.title}>
</React.Fragment>
);
}
}
export default Parent;


//Child

import React, { Component } from 'react';

class Child extends Component {
render(){
return (
<div>
<span className="title">{this.props.title}</span>
</div>
);
}
};
export default Child;









share|improve this question

























  • Where you have the curly braces - {this.state.title} - you can put any Javascript expression. If you are not familiar with splitting strings look at this documentation: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – Mixolydian
    Nov 26 '18 at 21:43














1












1








1








i'm working on a react project where i'm passing titles contained in states to a children element (see bottom).
How can i dynamically add to the first word (and only the first word cause my titles can contain 2 or 3 words) inside the this.state.title inside the child element?



//Parent

import React, { Component } from 'react';
import Child from './child';

class Parent extends Component {

state = {
title:'Admin Picture'
}

render() {
return (
<React.Fragment>
<Child title={this.state.title}>
</React.Fragment>
);
}
}
export default Parent;


//Child

import React, { Component } from 'react';

class Child extends Component {
render(){
return (
<div>
<span className="title">{this.props.title}</span>
</div>
);
}
};
export default Child;









share|improve this question
















i'm working on a react project where i'm passing titles contained in states to a children element (see bottom).
How can i dynamically add to the first word (and only the first word cause my titles can contain 2 or 3 words) inside the this.state.title inside the child element?



//Parent

import React, { Component } from 'react';
import Child from './child';

class Parent extends Component {

state = {
title:'Admin Picture'
}

render() {
return (
<React.Fragment>
<Child title={this.state.title}>
</React.Fragment>
);
}
}
export default Parent;


//Child

import React, { Component } from 'react';

class Child extends Component {
render(){
return (
<div>
<span className="title">{this.props.title}</span>
</div>
);
}
};
export default Child;






javascript reactjs ecmascript-6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 21:43









isherwood

37.6k1082114




37.6k1082114










asked Nov 26 '18 at 21:39









user3359675user3359675

184




184













  • Where you have the curly braces - {this.state.title} - you can put any Javascript expression. If you are not familiar with splitting strings look at this documentation: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – Mixolydian
    Nov 26 '18 at 21:43



















  • Where you have the curly braces - {this.state.title} - you can put any Javascript expression. If you are not familiar with splitting strings look at this documentation: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – Mixolydian
    Nov 26 '18 at 21:43

















Where you have the curly braces - {this.state.title} - you can put any Javascript expression. If you are not familiar with splitting strings look at this documentation: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

– Mixolydian
Nov 26 '18 at 21:43





Where you have the curly braces - {this.state.title} - you can put any Javascript expression. If you are not familiar with splitting strings look at this documentation: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

– Mixolydian
Nov 26 '18 at 21:43












1 Answer
1






active

oldest

votes


















1














You can do this in the Child component.



//Child

import React, { Component } from 'react';

class Child extends Component {
render() {
const splitTitle = this.props.title.split(' ');
const firstWord = splitTitle.shift();
const remainingTitle = splitTitle.join(' ');

return (
<div>
<span className="title">{firstWord}</span>
{remainingTitle}
</div>
);
}
};
export default Child;


Alternatively, you could also pass firstWord and remainingTitle to Child component as a prop. You would probably want to name them something different.






share|improve this answer
























  • Works really good, thank you!

    – user3359675
    Nov 26 '18 at 22:10











  • You may also want to separate the words with a space. You can add something like this {' '} between the <span> and {remaining title}

    – Tyler Becks
    Nov 26 '18 at 23:23














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%2f53489475%2fdynamically-add-a-span-tag-to-the-first-word-of-a-string%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









1














You can do this in the Child component.



//Child

import React, { Component } from 'react';

class Child extends Component {
render() {
const splitTitle = this.props.title.split(' ');
const firstWord = splitTitle.shift();
const remainingTitle = splitTitle.join(' ');

return (
<div>
<span className="title">{firstWord}</span>
{remainingTitle}
</div>
);
}
};
export default Child;


Alternatively, you could also pass firstWord and remainingTitle to Child component as a prop. You would probably want to name them something different.






share|improve this answer
























  • Works really good, thank you!

    – user3359675
    Nov 26 '18 at 22:10











  • You may also want to separate the words with a space. You can add something like this {' '} between the <span> and {remaining title}

    – Tyler Becks
    Nov 26 '18 at 23:23


















1














You can do this in the Child component.



//Child

import React, { Component } from 'react';

class Child extends Component {
render() {
const splitTitle = this.props.title.split(' ');
const firstWord = splitTitle.shift();
const remainingTitle = splitTitle.join(' ');

return (
<div>
<span className="title">{firstWord}</span>
{remainingTitle}
</div>
);
}
};
export default Child;


Alternatively, you could also pass firstWord and remainingTitle to Child component as a prop. You would probably want to name them something different.






share|improve this answer
























  • Works really good, thank you!

    – user3359675
    Nov 26 '18 at 22:10











  • You may also want to separate the words with a space. You can add something like this {' '} between the <span> and {remaining title}

    – Tyler Becks
    Nov 26 '18 at 23:23
















1












1








1







You can do this in the Child component.



//Child

import React, { Component } from 'react';

class Child extends Component {
render() {
const splitTitle = this.props.title.split(' ');
const firstWord = splitTitle.shift();
const remainingTitle = splitTitle.join(' ');

return (
<div>
<span className="title">{firstWord}</span>
{remainingTitle}
</div>
);
}
};
export default Child;


Alternatively, you could also pass firstWord and remainingTitle to Child component as a prop. You would probably want to name them something different.






share|improve this answer













You can do this in the Child component.



//Child

import React, { Component } from 'react';

class Child extends Component {
render() {
const splitTitle = this.props.title.split(' ');
const firstWord = splitTitle.shift();
const remainingTitle = splitTitle.join(' ');

return (
<div>
<span className="title">{firstWord}</span>
{remainingTitle}
</div>
);
}
};
export default Child;


Alternatively, you could also pass firstWord and remainingTitle to Child component as a prop. You would probably want to name them something different.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 21:57









Tyler BecksTyler Becks

636




636













  • Works really good, thank you!

    – user3359675
    Nov 26 '18 at 22:10











  • You may also want to separate the words with a space. You can add something like this {' '} between the <span> and {remaining title}

    – Tyler Becks
    Nov 26 '18 at 23:23





















  • Works really good, thank you!

    – user3359675
    Nov 26 '18 at 22:10











  • You may also want to separate the words with a space. You can add something like this {' '} between the <span> and {remaining title}

    – Tyler Becks
    Nov 26 '18 at 23:23



















Works really good, thank you!

– user3359675
Nov 26 '18 at 22:10





Works really good, thank you!

– user3359675
Nov 26 '18 at 22:10













You may also want to separate the words with a space. You can add something like this {' '} between the <span> and {remaining title}

– Tyler Becks
Nov 26 '18 at 23:23







You may also want to separate the words with a space. You can add something like this {' '} between the <span> and {remaining title}

– Tyler Becks
Nov 26 '18 at 23:23






















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%2f53489475%2fdynamically-add-a-span-tag-to-the-first-word-of-a-string%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

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen