我是React.js的新手,刚才我正在学习React中ref的概念。他们在V16.3中有新的createRefAPI。我试图从REACTDOC's中学习这个像这样-importReactfrom"react";exportclassMyComponentextendsReact.Component{constructor(props){super(props);//createareftostorethetextInputDOMelementthis.textInput=React.createRef();this.focusTextInput=this.focusTextInput.
我需要组件的引用。刚刚从jsx转移到tsx并找不到解决这个问题的方法。其他解决方法是使用查询选择器,但我认为这不是做react的好方法。这里是构造函数constructor(props){super(props);this.state={data:initialData,showNewListForm:false,whichButtonClicked:undefined,background:props.background||"#23719f"};this.divContainerRef=React.createRef();console.log("kanbanreference:"
基本上,我在React中使用constructor仅出于3个原因-1。初始化state就像-classAppextendsReact.Component{constructor(props){super(props);this.state={counter:0};}}但是由于Babel的class-field支持,我不用了classAppextendsReact.Component{state={counter:0};}2。bind功能如-classAppextendsReact.Component{constructor(props){super(props);this.increm
我正在尝试做this.我一定是遗漏了什么,但我不明白为什么在这个例子中current总是null。classAppextendsReact.PureComponent{constructor(props){super(props);this.test=React.createRef();}render(){returncurrentvalue:{this.test.current+""};}}可以查看我的测试用例here 最佳答案 因为您忘记将ref分配给某个dom元素。你只是在创造它。这样写:classAppextendsReac