我了解/相信我应该偏向功能组件而不是类组件,以便应用职责隔离。随着React Hooks的出现,它带来了一种避免Class的新方法,并且仍然具有功能保持状态。
我创建了一个非常简单的Web Flux休息服务。我使用Hooks和Axon创建了一个非常简单的React页面,并且得到了“ data.map不是RestApiHooksComponent的函数”。如果我尝试使用完全相同的代码来消耗典型的REST服务,它将正常运行(通过典型的REST服务,我的意思是没有任何响应功能的同步代码)。
我想我在使用Web Flux进行React数学时缺少一些基本想法。周围有几个示例显示如何通过React使用Webflux,但没有一个示例显示如何使用Hooks(至少我没有找到它)。 Hooks是否符合反应式范式?基于我的React有限的知识,我猜这将是一个很好的匹配:常见的用户案例正在使用来自流中的服务列表,并在可用时显示它。
前端
package.json
...
"dependencies": {
"axios": "^0.18.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "3.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
...
App.js
import React, { useEffect, useState } from "react";
import axios from "axios";
export default function RestApiHooksComponent() {
const [data, setData] = useState([]);
useEffect(() => {
axios
.get("http://127.0.0.1:8080")
.then(result => setData(result.data));
}, []);
return (
<div>
<ul>
{data.map(item => (
<li key={item.result}>
{item.result}: {item.result}
</li>
))}
</ul>
</div>
);
}
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));
LoanTable.js
import React from 'react'
const LoanTable = props => (
<table>
<thead>
<tr>
<th>Result</th>
</tr>
</thead>
<tbody>
{props.loans.length > 0 ? (
props.loans.map(loan => (
<tr>
<td>{loan.result}</td>
</tr>
))
) : (
<tr>
<td>No loans</td>
</tr>
)}
</tbody>
</table>
)
export default LoanTable
Webflux休息服务
控制器:
@RestController
public class LoansController {
@Autowired
private LoansService loansService;
@CrossOrigin
@RequestMapping(method = RequestMethod.GET, produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@ResponseBody
public Flux<Loans> findAll() {
Flux<Loans> loans = loansService.findAll();
return loans;
}
}
服务:
@Service
public class LoansService implements ILoansService {
@Autowired
private LoansRepository loansRepository;
@Override
public Flux<Loans> findAll() {
return loansRepository.findAll();
}
}
Webflux配置
@Configuration
@EnableWebFlux
public class WebFluxConfig implements WebFluxConfigurer
{
}
我没有显示的任何相关详细信息完全是无阻塞代码:
MongoDb配置
@Configuration
@EnableReactiveMongoRepositories(basePackages = "com.mybank.web.repository")
public class MongoConfig extends AbstractReactiveMongoConfiguration
{
@Value("${port}")
private String port;
@Value("${dbname}")
private String dbName;
@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
}
@Override
protected String getDatabaseName() {
return dbName;
}
@Bean
public ReactiveMongoTemplate reactiveMongoTemplate() {
return new ReactiveMongoTemplate(reactiveMongoClient(), getDatabaseName());
}
}
Pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mybank</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>web</name>
<description>webflux</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
***已编辑
网络流量服务输出
data:{"timestamp":1558126555269,"result":"8"} data:{"timestamp":1558132444247,"result":"10"} data:{"timestamp":1558132477916,"result":"10"} data:{"timestamp":1558132596327,"result":"14"}
答案 0 :(得分:1)
您的React组件运行正常。
尝试记录后端结果,然后再将其设置为useEffect的数据状态。问题是您的结果,它可能不是数组,并且map
函数仅在数组对象上可用(在这种情况下)。
useEffect(() => {
axios
.get("http://127.0.0.1:8080")
.then(result => {
const items = result.data // It should be an array to map
console.log(items)
if (items && items.length) {
setData(items)
}
});
}, []);