symfony / ux

Symfony UX initiative: a JavaScript ecosystem for Symfony
https://ux.symfony.com/
MIT License
853 stars 315 forks source link

React into Symfony NavLink #1271

Open LucasGreard opened 1 year ago

LucasGreard commented 1 year ago

Hi, I need to create a project with react and I've problems. I need to add NavLink to my component but when I build and see it, there are nothing. I use Symfony 6. Mycomponent.js :

import React from 'react';
import { NavLink } from 'react-router-dom';

export default function () {
    return (
    <header>
        <nav>
            <NavLink to="/">Accueil</NavLink>
            <div className="logo"></div>
            <div className='reseaux-sociaux'>
            <div className='youtube'></div>
            <div className='twitch'></div>
                <a href="#">
                    <div className='discord'></div>
                    <p>REJOINS NOUS</p>
                </a>
            </div>
        </nav>
    </header>);
}

My app.js :

import { registerReactControllerComponents } from '@symfony/ux-react';
import './bootstrap.js';
import { Route, Routes, Navigate} from 'react-router-dom';
/*
 * Welcome to your app's main JavaScript file!
 *
 * We recommend including the built version of this JavaScript file
 * (and its CSS file) in your base layout (base.html.twig).
 */

// any CSS you import will output into a single css file (app.css in this case)
import './styles/app.css';

registerReactControllerComponents(require.context('./react/controllers', true, /\.(j|t)sx?$/));

const App = () => {
    return (
        <>
            <Routes>
            <Route path='/' element={<Navigate replace to='/home' />} />
            <Route path='/Home' element={<Home />} />
            <Route path='/Champions' element={<Champions />} />
            <Route path='/Contact' element={<Contact />} />

            <Route path='/donjons/Araignee' element={<Araignee />} />
            <Route path='/donjons/ChevalierDuFeu' element={<ChevalierDuFeu />} />
            <Route path='/donjons/Dragons' element={<Dragons />} />
            <Route path='/donjons/GolemDeGlace' element={<GolemDeGlace />} />

            <Route path='/bosses/TourDeMalheur' element={<TourDeMalheur />} />
            <Route path='/bosses/BossDeClan' element={<BossDeClan />} />
            <Route path='/bosses/CrypteDeFaction' element={<CrypteDeFaction />} />
            <Route path='/bossess/LesJumeauxDeFer' element={<LesJumeauxDeFer />} />
            <Route path='/bosses/Hydre' element={<Hydre />} />

            <Route path='/arenes/AreneClassique' element={<AreneClassique />} />
            <Route path='/arenes/AreneEquipe' element={<AreneEquipe />} />

            <Route path='*' element={<NotFound />} /> 
            </Routes>
        </>
    );
};

export default App;

Webpack.config.js :

const Encore = require('@symfony/webpack-encore');

// Manually configure the runtime environment if not already configured yet by the "encore" command.
// It's useful when you use tools that rely on webpack.config.js file.
if (!Encore.isRuntimeEnvironmentConfigured()) {
    Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}

Encore
    // directory where compiled assets will be stored
    .setOutputPath('public/build/')
    // public path used by the web server to access the output path
    .setPublicPath('/build')
    // only needed for CDN's or subdirectory deploy
    //.setManifestKeyPrefix('build/')

    /*
     * ENTRY CONFIG
     *
     * Each entry will result in one JavaScript file (e.g. app.js)
     * and one CSS file (e.g. app.css) if your JavaScript imports CSS.
     */
    .addEntry('app', './assets/app.js')

    // When enabled, Webpack "splits" your files into smaller pieces for greater optimization.
    .splitEntryChunks()

    .enableReactPreset()

    // enables the Symfony UX Stimulus bridge (used in assets/bootstrap.js)
    .enableStimulusBridge('./assets/controllers.json')

    // will require an extra script tag for runtime.js
    // but, you probably want this, unless you're building a single-page app
    .enableSingleRuntimeChunk()

    /*
     * FEATURE CONFIG
     *
     * Enable & configure other features below. For a full
     * list of features, see:
     * https://symfony.com/doc/current/frontend.html#adding-more-features
     */
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    // enables hashed filenames (e.g. app.abc123.css)
    .enableVersioning(Encore.isProduction())

    // configure Babel
    // .configureBabel((config) => {
    //     config.plugins.push('@babel/a-babel-plugin');
    // })

    // enables and configure @babel/preset-env polyfills
    .configureBabelPresetEnv((config) => {
        config.useBuiltIns = 'usage';
        config.corejs = '3.23';
    })

    // enables Sass/SCSS support
    //.enableSassLoader()

    // uncomment if you use TypeScript
    //.enableTypeScriptLoader()

    // uncomment if you use React
    //.enableReactPreset()

    // uncomment to get integrity="..." attributes on your script & link tags
    // requires WebpackEncoreBundle 1.4 or higher
    //.enableIntegrityHashes(Encore.isProduction())

    // uncomment if you're having problems with a jQuery plugin
    //.autoProvidejQuery()
;

module.exports = Encore.getWebpackConfig();

When I've only html, it works

carsonbot commented 6 months ago

Thank you for this issue. There has not been a lot of activity here for a while. Has this been resolved?

carsonbot commented 5 months ago

Could I get an answer? If I do not hear anything I will assume this issue is resolved or abandoned. Please get back to me <3

carsonbot commented 5 months ago

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!