0

I am using switch case statement in which i am using two map function. one map function is used for word and other is used for punctuation mark. basically i want to split a word with punctuation mark. here i face this problem in which everything is going fine but my component in not render. in if else what should i do call this component whose name is A and B.

</div>
<script type="text/babel">
 class A extends React.Component {
    constructor(props){
      super(props);
        this.state={
        }
    }

    render(){
      return (
        <div>
         {this.props.item}hello
        </div>
      )
    }
 }
 class B extends React.Component {
    constructor(props){
      super(props);
    }
  render(){
    return(
      <div>
        {this.props.items}
      </div>
    )


  }
 }
 class C extends React.Component {
    constructor(props){
      super(props);
      self =this; 
      this.state = {
        cstate:true,
        renderdComp:''
      }
      this.switch =this.switch.bind(this)
      this.callComp =this.callComp.bind(this)
    }

callComp(item,punc,k){ console.log("item =>",item,"punc =>",punc,"k =>",k);

    if(punc == '') {
     this.setState({
         renderdComp : "A"
       })   
return <A item={item}/>

    } else {
         this.setState({
         renderdComp : "B"
       })
         return <B items={item}/>

    }}
switch(stateValue) {
if(stateValue) {
    let freshItem;
    let puncItem;
    ['a;','as','df','fg'].map( function(item, i) {
        [';',':','<'].map( function (punc, j) {
            const isFoundPunc = item.indexOf(punc)
            if(isFoundPunc > -1) {
                console.log(isFoundPunc)
                freshItem = item.substr(0,isFoundPunc);
                console.log(freshItem)
                puncItem = item.substr(isFoundPunc,item.length);
                console.log(puncItem)

            } else {
               freshItem = item
                console.log(freshItem+"sec")
                puncItem = ''
                 console.log(puncItem+"sec")

            }
        })
        self.callComp(freshItem, puncItem, i)
    })
} 

} render(){ return( {this.switch(this.state.cstate)} ) } } ReactDOM.render(,document.getElementById("container"));

3
  • You call this.switch inside render, but you don't return anything from this function, so nothing is rendered Commented Jan 30, 2018 at 11:59
  • so what should i do. Commented Jan 30, 2018 at 12:20
  • 1
    Try adding return before ['a;','as','df','fg'].map. And before self.callComp too. Commented Jan 30, 2018 at 12:23

1 Answer 1

0

You can setState here in this function

 constructor() {
     this.callComp = this.callComp.bind(this)
     }
    callComp(item,punc,k){
        console.log("item =>",item,"punc =>",punc,"k =>",k);

        if(punc == '') {
         this.setState({
             renderdComp : "A"
           })   
    return <A item={item}/>

        } else {
             this.setState({
             renderdComp : "B"
           })
             return <B items={item}/>

        }

This should rerender your component C

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

4 Comments

have you changed your function to arrow function. What error are you getting
No, I am not using arrow function. I am using normal map function. I am not getting any error but component is not render. can you plz do proper change in my code. please help will be appreciated
define callComp like the way i have defined using arrow function. Or you have to bind your function callComp in constructor like this this.callComp = this.callComp.bind(this)
nopes. That time infinite loop run. can you please give 2min and edit my real code. i updated as what you mention please look at my new updated code.

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.