https://github.com/owenmelbz/backpack-laravel-react-demo
A demo project of using React with Backpack for Laravel
https://github.com/owenmelbz/backpack-laravel-react-demo
backpack javascript laravel php react
Last synced: 3 months ago
JSON representation
A demo project of using React with Backpack for Laravel
- Host: GitHub
- URL: https://github.com/owenmelbz/backpack-laravel-react-demo
- Owner: OwenMelbz
- Created: 2019-03-17T12:24:10.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-03-17T12:24:35.000Z (about 6 years ago)
- Last Synced: 2024-12-31T04:13:45.419Z (5 months ago)
- Topics: backpack, javascript, laravel, php, react
- Language: PHP
- Size: 20.8 MB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Using React and Backpack for Laravel
This demo project contains 3 components to do different things.
1. A custom field input for forms.
2. A data source which updates a global store.
3. A display component which renders the output of the global store.## To install
- Clone the project to your computer
- Setup your database, vhosts etc within your .env
- Run `composer install` and `npm install` followed by `npm run dev`
- Run the laravel migrations with `php artisan migrate`## To use
- Create a Backpack user as normal, then login!
- Browse to the "User" CRUD and see the custom form element.
- Use the shared state input in the sidebar to manipulate the data store.## Steps taken
Each commit message contains basic instructions on what is going on during the commit.
Gradually it builds up the code base into what is shown here.
Below is a copy/paste of the steps taken - View the actual commits to see the code changes.
### Custom Field
#### Step 1
Add a new asset pipeline for your backpack assets using Laravel Mix
#### Step 2
Create your entry files e.g. backpack.js and backpack.scss
Within our CSS file we simply append some text for evidence that our CSS is loaded
Then we've run `npm run watch` which has generated our mix manifest, which we will use later!
#### Step 3
We publish the "scripts" blade template from the vendor folder to allow us to globally add our custom scripts
At the bottom after everything else has loaded we use the mix helper to import our CSS and our JS
We add it at the end for perceived performance and to make sure everything else has loaded.
#### Step 4
We add the field that we want to create into our Crud Controller - setting the "type" to the name of the new blade file we're going to create
#### Step 5
We replicate an existing input field to make sure we still have the ability to use things like "hints"
#### Step 6
We then reference the input field as a prop for our custom react component which we'll create later so we can update the value of the input via our Vue component.
#### Step 7
We then create our react component, doing what ever we need. This example imports an free "password strength" package which we interface with.
We are listening for the change event on line 17 and 8 to update the actual form element with the value returned from the password package.
At the bottom we create a loop which maps the html tag in our blade file "password-meter" to the above component passing in a reference to the input field.
We should now have a fully working custom field built with vue. If you're still running `npm run watch` this should have loaded for you.
We can confirm it's working in the next step.
#### Step 8
Here we just create a duplicate field, which displays the raw value of the database to prove that our component is updating the database!
### Shared state amongst UI components
#### Step 1
Shared State - As each react component runs in its own "root" this means if you want to share state between multiple components you must use a "store" which all required components subscribe to rather than passing down via props.
#### Step 2
Shared State - To simply display the output in the top left bar of the website.
We do this by creating a component which imports the store (using react-easy-state), we decorate it with easy states view() helper to make it reactive.
We register it as well in the backpack.js
Render the component by editing the published backpack template
#### Step 3
Shared State - Keeping it all in sync with other data sources.
We create a new component which also imports the store
This component however has a text box which allows us to mutate the data within the store.
We register it as well in the backpack.js
We also render it within our template!
You can now see, if you start typing in the sidebar which updates the global store, all other components which share this data will also update!
This allows you to easily keep data in sync across multiple components.