如何在React中正确使用useState钩子?

我有点在React中使用useState()钩子感到困惑。 我想以组件状态存储一些数据,例如表单中的值-name,minValue和maxValue。我的代码应该是什么样的?

import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import org.apache.xmlbeans.*;
import javax.xml.namespace.QName;

public class WordFillCheckBox {

 public static void main(String[] args) throws Exception {

  XWPFDocument document = new XWPFDocument(new FileInputStream("source.docx"));

  for (XWPFParagraph paragraph : document.getParagraphs()) { //go through all paragraphs
   for (CTSdtRun sdtRun : paragraph.getcTP().getSdtList()) {
    if (W14Checkbox.isW14Checkbox(sdtRun)) {
     W14Checkbox w14Checkbox = new W14Checkbox(sdtRun);
     System.out.println(w14Checkbox.getchecked());
     if (w14Checkbox.getchecked()) w14Checkbox.setChecked(false); else w14Checkbox.setChecked(true);
     System.out.println(w14Checkbox.getchecked());
    }
   }
  }

  FileOutputStream out = new FileOutputStream("result.docx");
  document.write(out);
  out.close();
  document.close();

 }

 static class W14Checkbox {
  CTSdtRun sdtRun = null;
  CTSdtContentRun sdtContentRun = null;
  XmlObject w14CheckboxChecked = null;

  W14Checkbox(CTSdtRun sdtRun) {
   this.sdtRun = sdtRun;
   this.sdtContentRun = sdtRun.getSdtContent();
   String declareNameSpaces = "declare namespace w14='http://schemas.microsoft.com/office/word/2010/wordml'";
   XmlObject[] selectedObjects = sdtRun.getSdtPr().selectPath(declareNameSpaces + ".//w14:checkbox/w14:checked");
   if (selectedObjects.length > 0) {  
    this.w14CheckboxChecked  = selectedObjects[0];
   }
  }
  CTSdtContentRun getcontent() {
   return this.sdtContentRun;
  }
  XmlObject getW14CheckboxChecked() {
   return this.w14CheckboxChecked;
  }
  boolean getchecked() {
   XmlCursor cursor = this.w14CheckboxChecked.newCursor();
   String val = cursor.getattributeText(new QName("http://schemas.microsoft.com/office/word/2010/wordml","val","w14"));
   return "1".equals(val) || "true".equals(val);
  }
  void setChecked(boolean checked) {
   XmlCursor cursor = this.w14CheckboxChecked.newCursor();
   String val = (checked)?"1":"0";
   cursor.setattributeText(new QName("http://schemas.microsoft.com/office/word/2010/wordml","w14"),val);
   cursor.dispose();
   CTText t = this.sdtContentRun.getRArray(0).getTArray(0);
   String content = (checked)?"\u2612":"\u2610";
   t.setStringValue(content);
  }

  static boolean isW14Checkbox(CTSdtRun sdtRun) {
   CTSdtPr sdtPr = sdtRun.getSdtPr();
   String declareNameSpaces = "declare namespace w14='http://schemas.microsoft.com/office/word/2010/wordml'";
   XmlObject[] selectedObjects = sdtPr.selectPath(declareNameSpaces + ".//w14:checkbox");
   if (selectedObjects.length > 0) return true;  
   return false;
  }
 }
}

keyboardType = .numbersAndPunctuation

这些方法中的任何一种还是更好的?

duke3032 回答:如何在React中正确使用useState钩子?

虽然两者都可以工作,但我认为第二个比较好,因为它更易于阅读和更新。如果要处理更复杂的对象,请查看useReducer()

,

useState()返回当前状态值和更新该状态值的方法。在那里您可以设置任何类型的对象,数组或字符串等。 像这样:

const [someState,setSomeState] = useState('something')                                    

要操纵状态,您必须像这样调用函数:

const someFunction = () => { setSomeState('new state') }                                              

希望有帮助。

,

两种方法都可以,但是,如果您有很多状态变量,则可以使用这种方法,因为它会减少代码

const [state,setState] = useState({
       name: '',minValue: 0,maxValue: 9
    });

    setState(prevData => ({
              ...prevData,name: value
              minValue: value
            }))

如果状态变量较少并且更易于管理,则可以使用这种方法

const [name,setName] = useState('');
const [minValue,setMinValue] = useState(0);
const [maxValue,setMaxValue] = useState(9);

setName(value);
setMinValue(value);
setMaxValue(value);
本文链接:https://www.f2er.com/3145023.html

大家都在问