OneTechSavvy / ether-dice

0 stars 0 forks source link

Implement Redux inside React/Intertia.js #2

Open OneTechSavvy opened 6 days ago

zacum commented 6 days ago

Implementing Redux inside a React/Inertia.js setup in a Laravel project involves several steps. Here's a detailed guide to help you get started:

Step 1: Set Up Laravel with Inertia.js

`import { configureStore } from '@reduxjs/toolkit'; import rootReducer from './rootReducer';

const store = configureStore({ reducer: rootReducer, });

export default store; `

`import { combineReducers } from 'redux'; // import your reducers here

const rootReducer = combineReducers({ // add your reducers here });

export default rootReducer; `

`import React from 'react'; import { render } from 'react-dom'; import { InertiaApp } from '@inertiajs/inertia-react'; import { Provider } from 'react-redux'; import store from './store';

const app = document.getElementById('app');

render(

require(`./Pages/${name}`).default} /> , app ); ` - Connect to Redux Create a sample component `resources/js/Pages/Home.js` `import React from 'react'; import { useSelector, useDispatch } from 'react-redux'; const Home = () => { const dispatch = useDispatch(); const state = useSelector(state => state); return (

Home

{JSON.stringify(state, null, 2)}
); }; export default Home; ` Set up a route for the component `use Inertia\Inertia; Route::get('/', function () { return Inertia::render('Home'); }); `
OneTechSavvy commented 6 days ago

Great, Thank you @zacum