0

I am facing a issue with one of my app. I am new to the react. I am using a laravel and react together. My problem is i am fetching data from a api which is a laravel controller. But the result which is coming in a html format but rendering in react it breaking the html codes. i have used parsers like import parse from 'html-react-parser'; import renderHTML from 'react-render-html';

but both are not working and it stop my result data also.

export default class Notify extends Component {

    state={
        duelist:[]
    }

    componentDidMount() {
        axios.get(`upcomingdues`)
          .then(res => {
            const duelist = res.data;
            //console.log(res.data);
            this.setState({ duelist });
          })
      }

    render() {
        const mystyle = {
            height: "300px",
            overflow: "hidden"
        };
        const mystyle1 = {
            left: "0px",
            bottom: "0px"
        };
        const mystyle2 = {
            left: "0px",
            width: "0px"
        };
        return (
            <div> 
               <div className="tab-content">
                <div className="tab-pane active show" id="topbar_notifications_notifications" role="tabpanel">
                 <div className="kt-notification kt-margin-t-10 kt-margin-b-10 kt-scroll ps" 
                 data-scroll="true" data-height="300" data-mobile-height="200" style={mystyle}>

               {this.state.duelist}





                </div>
                </div>
                </div>
                <div className="ps__rail-x" style={mystyle1}>
                <div className="ps__thumb-x" tabindex="0" style={mystyle2}></div>
                </div>
            </div>                              
        );
    }
}

if (document.getElementById('notify')) {
    ReactDOM.render(<Notify />, document.getElementById('notify'));
}

2 Answers 2

1

Its resolve by me.

import parse, { domToReact } from 'html-react-parser';

i used react parser and my issue in the above code was react set the state , but after setting the state i am parsing the state. So what i did now is

componentDidMount() {
        axios.get(`upcomingdues`)
          .then(res => {
            const duelist = parse(res.data);
            //console.log(res.data);
            this.setState({ duelist });
          })
      }

parse it before setting the state and render, and all it works perfectly for me.

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

Comments

0

Please share your laravel code too. I think the problem is if you write the route in route/api.php. In that case you need to add api before the route name e.g. http://google.com/api/upcomingdues.

2 Comments

Well it does not do anything with route/api.php. Result or the output is coming from the ajax call by using the react component. But the only difference is html is broken and its not coming correctly.
This is the output material coming : a href="#" class="kt-notification__item"> <div class="kt-notification__item-icon"> <i class="flaticon2-line-chart kt-font-success"></i> </div> <div class="kt-notification__item-details"> <div class="kt-notification__item-title"> ID no is 30 and Student name is testingg testingg </div> <div class="kt-notification__item-time">2 hrs ago</div> </div> </a>

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.