Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

React: Split String in Render HTML

23.02.2021 (👁578)


 

Fehlermeldung:

TypeError: Cannot read property 'split' of null

 

 

Lösung:

Ein Fragezeichen hinter der Variable vor dem split() einfügen

(this.state.keywords)?.split(";").map(keyword =>

<a href={this.baseURL + "?k=" + keyword}> {keyword}</a>                                            

 

 

Um eine Null Variable zu verhinden, kann man ein ? hinter eine Variable und einer Funktion anfügen, wodurch diese nur noch ausgeführt wird, wenn ein Wert vorhanden ist.

 

TypeError: Cannot read property 'split' of null

Article.render

D:/Programmierung/Web/Demo/CodeDocu/CodeDocu/ClientApp/src/components/Article.js:98

   95 |                                </div>
 
  96 |
 
  97 |                                <p>Keywords: {this.state.keywords}</p>
>  98 |                                <p>Keywords:
 
     | ^   99 |                                {
 
 100 |                                        (this.state.keywords).split(";").map(keyword =>
 
 101 |                                            <a href={this.baseURL + "?k=" + encodeURI(keyword)} style={{ margin: '4px' }}>{keyword}</a>                                            


 
 56 |            iduser      :data.idUser,
 
 57 |            title       :data.title,

View compiled

This screen is visible only in development. It will not appear if the app crashes in production.
Open your browser’s developer console to further inspect this error. Click the 'X' or hit ESC to dismiss this message.

 

 

 

 

 

React Code with .split(";").map..