0

I'm trying to add Laravel existing conditional statement on ReactJs component

I've tried to add it on render function.

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

export class Login extends Component {
    render() {
        return (
            <div class="login">
                @if (Route::has('login'))
                    <div class="top-right links">
                        @auth
                            <a href="{{ url('/home') }}">Home</a>
                        @else
                            <a href="{{ route('login') }}">Login</a>
                            /*-- <a href="{{ route('register') }}">Register</a>
                                @if (Route::has('register'))
                            @endif */
                        @endauth
                    </div>
                @endif
            </div>
        );
    }
}

if (document.getElementById('login')) {
    ReactDOM.render(<Login />, document.getElementById('login'));
}
<div id="login"> </div>
require('./components/Welcome');

I'm trying to display the existing laravel script from react components.

Thanks in advance for the help.

2
  • 1
    @if is used by blade template engine and react use jsx or ts which is not possible to use blade template codes try to use js if condition Commented Sep 7, 2019 at 8:36
  • This problem were months ago. Thanks for this information sid Commented Nov 26, 2019 at 15:32

1 Answer 1

1

@if use by blade template which output is <?php if(condition) ?>

you can try this

import React, { Component } from 'react';
import ReactDOM from 'react-dom';

export class Login extends Component {
    render() {
        return (
            <div class="login">

                    <div class="top-right links">
                       if (isLoggedIn) {
                            <a href="/home">Home</a>
                        } else {
                            <a href="/login">Login</a>

                        }
                    </div>

            </div>
        );
    }
}

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

isLoggedIn is coming from state of react where should true when user is logged in

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

1 Comment

Sorry It was weeks ago, I forgot this problem on my notes. Thanks anyway for the help.

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.