简体   繁体   English

redux 工具包状态重置

[英]redux toolkit state resets

I am stuck setting a state in react redux tool kit.我被困在 react redux 工具包中设置状态。 My objective is to set the state by capturing some information from a form.我的目标是通过从表单中捕获一些信息来设置状态。

Store店铺

import { createSlice } from '@reduxjs/toolkit'

export const SourceSlice = createSlice({
  name: 'source',
  initialState: {
    name : "" ,
    description:"",
    version:"" ,
    contact :"" ,
    id:"",
  } ,

  reducers: {

    addSource: (state ,action) => {
      state.name = action.payload.name
      state.description = action.payload.description
      state.version = action.payload.version
      state.contact = action.payload.contact
      state.id =  action.payload.name + "-" + action.payload.version
      } ,
  },
})


// Action creators are generated for each case reducer function
export const { addSource ,addExtract} = SourceSlice.actions
export default SourceSlice.reducer

Form形式

import { useDispatch } from 'react-redux'
import {addSource  } from '../../Store/SourceSlice'
import React, {useState} from 'react'

const Source = () => {

    const dispatch = useDispatch("")

    const [name, setName] = useState('');
    const [description, setDescription] = useState('');
    const [version, setVersion] = useState('');
    const [contact, setContact] = useState('');
    
    const saveSourceDetails = () => {      
      const payload = 
        { 
            name : name,
            description : description,
            version: version ,
            contact: contact
        }      
      dispatch(addSource(payload))
    };


    const onDataChange = (event) => {
      event.preventDefault();     

      if( event.target.name === "name"){
          setName(event.target.value)
      }
      
      if( event.target.name === "description"){
          setDescription(event.target.value)
      }       
      if( event.target.name === "version"){
          setVersion(event.target.value)
      }    
      if( event.target.name === "contactPerson"){
          setContact(event.target.value)
      }  

    }

    return (
      <div>
      <form className="ui form" onSubmit={saveSourceDetails}>
        <div className="field">
          <div className="Three fields">
            <div className="field">
              <input
                type="text"
                name="name"
                value={name}
                placeholder="Source Name"
                onChange={onDataChange}
              />
            </div>
            <div className="field">
              <input
                type="text"
                name="description"
                value={description}
                placeholder="Source Description"
                onChange={onDataChange}
              />
            </div>
            <div>
              <select
                name="version"
                multiple=""
                className="ui fluid dropdown"
                onChange={onDataChange}
              >
                <option value=""></option>
                <option value="v1">v1</option>
                <option value="v2">v2</option>
                <option value="v3">v3</option>
              </select>
            </div>
            <div className="field">
              <input
                type="text"
                name="contactPerson"
                value={contact}
                placeholder="Contact Person"
                onChange={onDataChange}
              />
            </div>
            <button className="ui button" type="submit">
              +
            </button>
          </div>
        </div>
      </form>
    </div>
    );
};

export default Source;

Every time when i hit the button , the data is captured, send to redux the state is set for a second and the then state is reset to initial state.每次当我点击按钮时,数据被捕获,发送到 redux 状态设置一秒钟,然后状态重置为初始状态。 I am struggling to find out what makes the tool to reset everything.我正在努力找出是什么使该工具可以重置所有内容。 Any leads would help me.任何线索都会帮助我。

PS: I am new to javascript and react PS:我是 javascript 新手并做出反应

This function is the culprit.这个函数是罪魁祸首。 When you send the form, this gets called.当您发送表单时,它会被调用。 You are not preventing the default event, so the page reloads, and you lose your Redux store.您没有阻止默认事件,因此页面重新加载,并且您丢失了 Redux 存储。

const saveSourceDetails = () => {
  const payload = {
    name: name,
    description: description,
    version: version,
    contact: contact
  };
  dispatch(addSource(payload));
};

Here's the corrected version:这是更正后的版本:

const saveSourceDetails = (e) => {
  e.preventDefault();
  const payload = {
    name: name,
    description: description,
    version: version,
    contact: contact
  };
  dispatch(addSource(payload));
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM