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 (πŸ‘9526)

React: Split String in Render HTML

 

Error message:

TypeError: Cannot read property 'split' of null

 

 

Solution:

Insert a question mark after the variable before split()

 

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

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

 

 

To avoid a zero variable, you can use a ? append behind a variable and a function, which will only run it if a value exists.

 

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..