spring-boot react redux增删改查

环境准备

spring-boot react一步一步实现增删改查 组件化为基础代码,在gis分支的基础上,创建一个redux分支,来集成redux组件,实现增删改查

  1. 检出代码
git clone https://gitee.com/qinaichen/react-crud.git

cd react-crud
  1. 切换分支
git checkout gis
  1. 创建redux分支
git checkout -b redux

添加redux

cd web

npm install redux --save

创建store

  1. src目录下创建store文件夹,并创建index.js
import { createStore } from 'redux'

const store = createStore();

export default store;
  1. store目录下创建reducer.js
const defaultState = {
    id:'',
    name:'',
    list:[]
}
export default (state = defaultState,action)=>{
	return state;
}
  1. store中引入reducer
import { createStore } from 'redux'
import reducer from './reducer'

const store = createStore(reducer);

export default store;

store与组件结合使用

  1. store引入App.js组件
import store from './store'
  1. 使用store中的数据对组件中的state进行初始化,并对store的数据进行订阅,当store中的数据发生变化时,组件中的数据也发生相应的变化
constructor(props) {
	super(props);
	this.state = store.getState();
	store.subscribe(()=>{
		this.setState(store.getState());
	})
}
  1. 修改App.js中的处理逻辑
edit = (item) => {
	const action = {
		type:'edit_user_name',
		user:item
	}
	store.dispatch(action)
}

query = () => {
   axios.get('/user').then(({data})=>{
       const action = {
           type:'init_user_list',
           list:data
       };
       store.dispatch(action);
   })
}
handleChange = (name) =>{
   const action = {
       type: 'change_user_name',
       name
   };
   store.dispatch(action);
}

handleFormSubmit = (e) => {
   e.preventDefault();
   if(this.state.name !== ''){
        axios.post('/user',{
            id:!this.state.id?'':this.state.id,
            name:this.state.name
        }).then(({data})=>{
            const action = {
                type:'set_user_empty',
                user:{id:'',name:''}
            }
            store.dispatch(action);
            this.query();
        })
   }
}
  1. reducer中添加相应的处理逻辑
/**
 * 表单控件修改逻辑
 */
if(action.type === 'change_user_name'){
    const newState = Object.create(state);
    newState.name = action.name;
    return newState;
}
/**
 * 初始化list
 */
if(action.type === 'init_user_list'){
    const newState = Object.create(state);
    newState.list = action.list;
    return newState;
}
/**
 * 编辑用户
 */
if(action.type === 'edit_user_name'){
    const newState = Object.create(state);
    const {id,name} = action.user;
    newState.id = id;
    newState.name = name;
    return newState;
}
/**
 * 将state中的id和name设置为空
 */
if(action.type === 'set_user_empty'){
    const newState = Object.create(state);
    const {id,name} = action.user;
    newState.id = id;
    newState.name = name;
    return newState;
}

源码https://gitee.com/qinaichen/react-crud.git 中的redux分支

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot是一个用于开发Java应用程序的框架,它简化了Java开发的很多繁琐过程,提供了快速搭建应用的能力。React是一个用于构建用户界面的JavaScript库,它能够帮助我们构建可复用的UI组件。 要在Spring Boot项目中集成React,可以按照以下步骤进行操作: 1. 创建一个基于Spring Boot的项目,可以使用Spring Initializer快速创建一个空白项目结构。 2. 安装Node.js并使用npm来管理项目依赖。在项目根目录下创建一个新的目录作为前端代码的根目录。 3. 使用npm安装React的依赖:运行`npm install react react-dom`命令。 4. 创建一个入口HTML文件,可以命名为`index.html`,并将其放置在前端代码根目录中。在该HTML文件中,引入React的核心库文件。 5. 创建一个入口JavaScript文件,可以命名为`index.js`,并将其放置在前端代码根目录中。在该JavaScript文件中,编写React组件的代码逻辑。 6. 使用Webpack等前端构建工具,将React的代码编译打包成静态资源文件。配置Webpack的配置文件,将入口JavaScript文件作为打包的入口。 7. 在Spring Boot项目中创建一个RESTful API接口,用于处理前端发送的请求。可以使用Spring MVC来实现这些接口。 8. 在Spring Boot项目中的Controller层中,处理前端发送的增删改查的请求,并调用对应的服务层方法进行具体的业务处理。 9. 在前端代码中,通过Fetch API或者Axios等工具,发送HTTP请求到Spring Boot项目的RESTful API接口,实现与后端的数据交互。 10. 在页面中显示查询到的数据,并提供增删改操作的界面。可以使用React的组件库来构建界面,或者自行编写组件。 通过以上步骤,我们就可以在Spring Boot项目中集成React,实现增删改查的操作。前端使用React构建用户界面,发送HTTP请求到后端的Spring Boot项目中,后端根据请求的类型进行相应的数据操作,再将结果返回给前端进行显示。这样就完成了一个基本的增删改查功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心扬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值