I have an array of span
elements that I need to render inside another static text.
Example final result: Put 3 , 1 and 2 in the right order here the bold numbers are the spans
Here is my code
generateQuestion() {
const newState = { ...this.state };
let numbers= newState.numbers.map((item, index) => <span style={{color: "red"}}>{item.number}</span>)
let textQuestion = `Place the numbers ${numbers[0]}, ${numbers[1]} and ${numbers[2]} in the right order.`;
return (
<div>{textQuestion}</div>
);
}
The problem is I'm getting this at render time Place the numbers [object Object], [object Object] and [object Object] in the right order
When I render numbers
array directly I am getting the spans
return (
<div>{numbers}</div>
);
How can I solve this issue?
You are trying to render objects inside string, the right way is to use jsx instead of string:
generateQuestion() {
const newState = { ...this.state };
let numbers= newState.numbers.map((item, index) => <span style={{color: "red"}}>{item.number}</span>)
let textQuestion = <div>Place the numbers {numbers[0]}, {numbers[1]} and {numbers[2]} in the right order.<div>;
return (
{textQuestion}
);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.