1

I have a similar question to this question

I am trying to convert create a custom components where it convert each string to difference color for example

input "A B C D"

output A B C D (cant chance color in this question, so I used Bolt and emphasis instead, same logic)

Currently I have

//In component
function MakeColor(){
  var input = [
    {letter:"A",color:"Red"},
    {letter:"B",color:"Blue"},
    {letter:"C",color:"Orange"},
    {letter:"D",color:"Yellow"}
  ];
  var output ="";
  input.forEach(function(object){
    output=output+"<span className="+object.color+">"+object.letter+"</span>";
  })
  return React.createElement('div',null,output)
}
//In another component
class Display extends Component {
   componentDidMount(){
      this.setState({letter:MakeColor();})
   }
   render(){
      <div>{this.state.letter}</div>
   }
}
//In the outer component
...
render(){
  return(
     ...
     <Display />
     ...
  )
}
...

This will return the text <span className=Red>A</span><span className=Blue>B</span><span className=Orange>C</span><span className=Yellow>D</span> instead of ABCD

1
  • Instead of having it as string have it as array, i.e output.push(...) push the value in each iteration instead of appending Commented Jul 1, 2019 at 9:16

3 Answers 3

1

Well you are mixing a lot of concepts.

  1. You don't need state
  2. Use JSX instead of React.createElement, you don't need it with this case.

    function Display() { return makeLetter();
    }

The makeLetter function:

const LETTERS = [
  { letter:"A",color:"Red" },
  { letter:"B",color:"Blue" },
  { letter:"C",color:"Orange" },
  { letter:"D",color:"Yellow" }
];

function makeLetter() {
  return LETTERS.map((obj) => <span className={obj.color}>{obj.letter}</span>);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Why not just use JSX instead?

let input = [
  { letter: 'A', color: 'Red' },
  { letter: 'B', color: 'Blue' },
  { letter: 'C', color: 'Orange' },
  { letter: 'D', color: 'Yellow' }
];

let output = input.map(item => (
  <span className={item.color}>{item.letter}</span>
));

return (
  <div>{output}</div>
);

Or even more succinctly:

let input = [...];  // As before

return (
  <div>
    {input.map(item => (<span className={item.color}>{item.letter}</span>))}
  </div>
);

Comments

0

Here is my observation:

1) You are not usings styles /css classes, neither you have used styles attribute.

So you code should be something like:

  var input = [
    {letter:"A",style: {color:"red"}},
    {letter:"B",style: {color:"blue"}},
    {letter:"C",style: {color:"orange"}},
    {letter:"D",style: {color:"yellow"}}
  ];

and also at below line, you can use JSX to return instead of a string:

 output=output+"<span className="+object.color+">"+object.letter+"</span>";

Try these points, please

Comments

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.