redux 单选按钮仅在单击两次时更改

kzmpq1sx  于 2022-11-12  发布在  其他
关注(0)|答案(1)|浏览(115)

使用的语言:带有react / redux工具包的Javascript,带有react挂钩形式的npm包。
形势:我有一个形式,其中一个步骤是由三个单选按钮选择一种语言.点击我分派状态到我的减速器(工作)
我的问题:当我单击一次单选按钮时,状态发生了变化,我可以在redux devTools中看到它。但是我需要单击两次才能直观地看到变化。
这是我的网站

import React from 'react';
import { Language} from './Language';
import { useDispatch, useSelector } from 'react-redux';

import {
   chooseLanguage,
 } from '../../../features/language.slice';

import { useForm } from 'react-hook-form';

export const StepOne = () => {
  const dispatch = useDispatch();
  const formDatas = useSelector((state) => state.form);

  const { register, handleSubmit } = useForm();

  const onChange = (data) => {
    dispatch(chooseLanguage(data.language));
   };

  return (
    <div>
      <h2 className="title"> Choose a language</h2>
      <form onChange={handleSubmit(onChange)} className="form">
        <Language register={register} formDatas ={formDatas} />
        </form>
    </div>
  );
};

这是包含单选按钮的子组件。

import React from 'react';

export const Language = ({ register, formDatas}) => {
  const defaultLanguage = formDatas.language;

  return (
    <div className="form_group">
      <label className="subtitle">language list</label>
      <div className="input">
        <div className="input_group">
          <label className="input_label">EN</label>
          <input
            {...register('language')}
            name="language"
            type="radio"
            value="EN"
            defaultChecked={defaultLanguage === 'EN'}
          />
        </div>

        <div className="input_group">
          <label className="input_label">FR</label>
          <input
            {...register('language')}
            name="language"
            type="radio"
            value="FR"
            defaultChecked={defaultLanguage === 'FR'}
          />
        </div>

        <div className="input_group">
          <label className="input_label">IT</label>
          <input
            {...register('language')}
            name="language"
            type="radio"
            value="IT"
            defaultChecked={defaultLanguage === 'IT'}
          />
        </div>
      </div>
    </div>
  );
};

我怎样才能在第一次单击时看到选中的值?

已解决

我的索引.js:

import React, {useState, useEffect} from 'react';
  import { Language} from './Language';
  import { useDispatch, useSelector } from 'react-redux';
  import {chooseLanguage} from '../../../features/language.slice';

  import { useForm } from 'react-hook-form';

    export const StepOne = () => {

      const dispatch = useDispatch();
      const formDatas = useSelector((state) => state.form);
      const [language, setLanguage] = 
      useState(formDatas.language);
      const handleChange = (e) => {
      setLanguage(e.target.value); };

      useEffect(() => {
       dispatch(chooseLanguage(language));
        }, [language, dispatch]);

      return (
        <div>
          <h2 className="title"> Choose a language</h2>
            <Language        
             handleChange={handleChange}
             language={language}
             setLanguage={setLanguage}
             formDatas ={formDatas} />

        </div>
      );
    };

这是包含单选按钮的我的子组件:

import React from 'react';

export const Language = ({ handleChange, formDatas}) => {
  const defaultLanguage = formDatas.language;

  return (
    <div className="form_group">
      <label className="subtitle">language list</label>
      <div className="input">
        <div className="input_group">
          <label className="input_label">EN</label>
          <input
             name="language"
            type="radio"
            value="EN"
            onChange={handleChange}
            defaultChecked={defaultLanguage === 'EN'}
          />
        </div>

        <div className="input_group">
          <label className="input_label">FR</label>
          <input
             name="language"
            type="radio"
            value="FR"  
            onChange={handleChange}
            defaultChecked={defaultLanguage === 'FR'}
          />
        </div>

        <div className="input_group">
          <label className="input_label">IT</label>
          <input
             name="language"
            type="radio"  
            onChange={handleChange}
            value="IT"
            defaultChecked={defaultLanguage === 'IT'}
          />
        </div>
      </div>
    </div>
  );
};
cnjp1d6j

cnjp1d6j1#

Index.js

import "./styles.css";
import { useForm } from "react-hook-form";
import { Language } from "./Languaje";

export default function App() {
  const { register, handleSubmit } = useForm();
  const formDatas = true; // I dont have redux in this project

  const onSubmit = (json) => {
    console.log(json); // expect the value of radio
  };
  return (
    <div className="App">
      <form onSubmit={handleSubmit(onSubmit)}>
        <Language register={register} formDatas={formDatas} />
        <button type="submit">Submit</button>
      </form>
    </div>
  );
}

仅将表单“onChange”的属性更改为“onSubmit”,并添加按钮类型“submit”,以允许发送数据。

相关问题