Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Part 1 tutorial does not mention how to bootstrap/inject React into the project #124

Open
asiupt opened this issue Jun 1, 2020 · 6 comments

Comments

@asiupt
Copy link

asiupt commented Jun 1, 2020

I am following the Do-It-Yourself version of the tutorial and the Part 1 tutorial does not mention how to bootstrap/inject React into the project.

I have included "frontend-maven-plugin" in my pom.xml and updated my project. Upon running, localhost:8080/api displays the correct JSON data, as expected. HOWEVER, localhost:8080 displays a blank page. Therefore, React components are not working.

I cannot see package.json or webpack.config.js in my class path.

Please tell me how to include React components so the tables can be displayed.

@samuelpucat
Copy link

samuelpucat commented Jun 19, 2020

i got it working plugin setup like this:

<plugin>
				<groupId>com.github.eirslett</groupId>
				<artifactId>frontend-maven-plugin</artifactId>
				<version>1.9.1</version>
				<configuration>
					<installDirectory>target</installDirectory>
				</configuration>
				<executions>
					<execution>
						<id>install node and npm</id>
						<goals>
							<goal>install-node-and-npm</goal>
						</goals>
						<configuration>
							<nodeVersion>v12.14.0</nodeVersion>
							<npmVersion>6.13.4</npmVersion>
						</configuration>
					</execution>
					<execution>
						<id>npm install</id>
						<goals>
							<goal>npm</goal>
						</goals>
						<configuration>
							<arguments>install</arguments>
						</configuration>
					</execution>
					<execution>
						<id>webpack build</id>
						<goals>
							<goal>webpack</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

@marcelovidigal
Copy link

marcelovidigal commented Jul 22, 2020

I had a series of problems, mainly with the webpack, and also with the client rest.js. I believe that a more detailed explanation of rest clients, other options, and especially rest.js client would help. Anyway, in the end, after a lot of struggle, I fixed as in the previous answer, however, not directly, nor even in the first attempt, I can't explain why. What I did initially: I deleted the target folder, used the following code in pom.xml, and finally ran the command line ./mvnw clean install and then ran ./mvnw spring-boot:run. I looked at the log, and finally there were no npm installation errors, and no webpack errors at all. In a complementary way I had to change some lines in the app.js, specifically when using client.js rest client, which also had errors. After all this, I decided to delete the target folder again, and see if the simple update of the pom.xml file, with the addition of the plugin, as in the code, would work, and to my surprise it worked normally.

In pom.xml <plugins> I use this code:

			<plugin>
				<groupId>com.github.eirslett</groupId>
				<artifactId>frontend-maven-plugin</artifactId>
				<version>1.9.1</version>
				<configuration>
					<installDirectory>target</installDirectory>
				</configuration>				
				<executions>
					<execution>
						<id>install node and npm</id>
						<goals>
							<goal>install-node-and-npm</goal>
						</goals>
						<configuration>
							<nodeVersion>v12.18.2</nodeVersion>
							<npmVersion>6.14.5</npmVersion>
						</configuration>
					</execution>
					<execution>
						<id>npm install</id>
						<goals>
							<goal>npm</goal>
						</goals>
						<configuration>
							<arguments>install</arguments>
						</configuration>
					</execution>
					<execution>
						<id>webpack build</id>
						<goals>
							<goal>webpack</goal>
						</goals>
					</execution>
				</executions>				
			</plugin>

In app.js I use this code, replacing done with then :

	componentDidMount() {
		client({method: 'GET', path: '/api/resource'}).then(response => {
			this.setState({resources: response.entity._embedded.resources});
		});
	}

In package.json I use the latest version for each dependency:

	"dependencies": {
		"react": "^16.13.1",
		"react-dom": "^16.13.1",
		"rest": "^2.0.0"
	},
	"scripts": {
		"watch": "webpack --watch -d --output ./target/classes/static/built/bundle.js"
	},
	"devDependencies": {
		"@babel/core": "^7.10.5",
		"@babel/preset-env": "^7.10.4",
		"@babel/preset-react": "^7.10.4",
		"babel-loader": "^8.1.0",
		"webpack": "^4.43.0",
		"webpack-cli": "^3.3.12"
	}

@liposo
Copy link

liposo commented Jul 29, 2020

@asiupt react is installed by npm install executed from:
<execution> <id>npm install</id> <goals> <goal>npm</goal> </goals> <configuration> <arguments>install</arguments> </configuration> </execution>
it will install all depenencies described in the package.json.

The package.json or webpack.config.js you need to create the files by yourself or you can run npm init Creating a package.json file, for webpack.config.js there is also an option to generate it using webpack-cli

UgmaDevelopment added a commit to UgmaDevelopment/tut-react-and-spring-data-rest that referenced this issue Jan 14, 2021
It looks like, to maintain sanity, the configuration for the
frontend-maven-plugin was defined in the root tutorial pom and the
configuration is inherited by all the submodules, like `basic`.

The problem was that one following along with the project needs that
configuration.

Now, we show the full configuration instead by including the
frontend-maven-plugin configuration from the root POM instead of
from `basic`'s POM.

Should resolve some concerns described in issues  spring-attic#124, spring-attic#128.
@benhunter
Copy link

This pull request: #128 should resolve issues with pom.xml related to frontend-maven-plugin and React.

@edtbl76
Copy link

edtbl76 commented Nov 24, 2021

I have the same problem. When visiting localhost:8080, I get a page with the word "index". The React components aren't rendered.

@edtbl76
Copy link

edtbl76 commented Nov 24, 2021

Nope. Found the issue. @marcelovidigal had the right idea swapping out done() for then()
(You also have to change the devtools in the webpack.config.js to a Webpack 5 compliant option. 'eval-source-map' worked for me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

6 participants