1

To setup my issue: I want to sort a text-document with regions of texts. the structure of data is:

{"words": ["Horn ", "Cirque ", "Glacier in Longitudinal Section "] }
{"words": ["my other ", "line ", "of texts"] }

inside an array. The result must be rendered in React using a webComponent for each sentence:

textContent.map((sentences, index) =>
              <article>
               <desc-sentence>{sentences}</desc-sentence>
              </article>

          )

textContent is the variable containing each sentence in regions.

but Objects are not valid as Children in react. An array is way recommanded, how to pass array with indexes ?

*Edit

the output shoud be

let textContent = [
     {"words": ["Horn ", "Cirque ", "Glacier in Longitudinal Section "]},
     {"words": ["V-shaped valley ", "carved by a river "]}
]

console.log(textContent[0].words);
<section>
<!-- inside the textContent loop of words-->
<article>
  <p>{sentence}</p>  
  <p>{sentence}</p>
 </article>
 <article>
  <p>{sentence}</p>  
  <p>{sentence}</p>
 </article>
</section>

6
  • What is the expected/wanted output? Can you write the wanted output html after the render? Commented Mar 5, 2018 at 13:55
  • Can you show the structure of textContent Commented Mar 5, 2018 at 13:56
  • 1
    What does sentences look like? Commented Mar 5, 2018 at 13:56
  • I have now set an exemple of output and structure of textContent Commented Mar 5, 2018 at 14:15
  • @aurny2420289 Do you want it to be like: <article> <p>Horn</p> <p>Cirque</p> <p>Glacier in Longitudinal Section</p> </article> ? Commented Mar 5, 2018 at 14:38

1 Answer 1

1

Here is the same code based on your requirement.You can check the HTML structure I think it's mostly what you want tag has all the names wrapped it in

import React from 'react'
class TestJS extends React.Component {
    render(){
        let textContent = [
            {"words": ["Horn ", "Cirque ", "Glacier in Longitudinal Section "]},
            {"words": ["V-shaped valley ", "carved by a river "]}
        ];
        
        let finalStringMessage = [];
        let textContentLength = textContent.length;
        for(let i=0; i < textContentLength; i++){
            let wordsArray = textContent[i].words;
            let articleName = wordsArray.map((sentences, index) =>
                    <desc-sentence>{sentences}</desc-sentence>

            );
            finalStringMessage.push(<article>{articleName}</article>)
        }
        return(
            <div>{finalStringMessage}</div>
        )
    }
}

export default TestJS;

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

1 Comment

Thank you, it's the answer i was looking for. is it possible to apply this code to ReactDOM.render( <section>{finalStringMessage}</section>, document.getElementById('texte-image') ); ?

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.