javascript - ReactJS:React-Month-Picker 错误:函数组件不能有引用。你的意思是使用 React.forwardRef() 吗?
问题描述
我正在使用"react": "^16.12.0"
版本,我制作了 2 个单独的屏幕,一个是基于 React 组件的,另一个是基于函数的。
在基于 React-component 的屏幕月份选择器中为我工作,如下所示,
import React, { Component } from 'react';
import {Button} from 'react-bootstrap';
import Picker from 'react-month-picker';
import 'react-month-picker/css/month-picker.css';
import MonthBox from './MonthPicker';
class OldScreen extends Component{
state = {
Durtn: "",
monthValue: {year: new Date().getFullYear(), month: (new Date().getMonth())},
FormatedMonth:"",
}
selectDuration(){
console.log("Generate")
// this.setState({Durtn: "Dur"});
}
handleClickMonthBox(e) {
this.refs.pickAMonth.show()
}
handleAMonthChange(year, month) {
let Dur = {year: year, month:month}
this.setState( {monthValue: Dur} )
}
handleAMonthDissmis(value) {
this.setState( {monthValue: value} )
}
render(){
const pickerLang = {
months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
}
,mvalue = this.state.monthValue
let makeText = m => {
if (m && m.year && m.month) return (pickerLang.months[m.month-1] + '. ' + m.year)
return '?'
}
return(
<div style={{ padding:15}}>
<form>
<div className="form-row" style={{ paddingTop:20}}>
<div className="edit" style={{width:100,paddingLeft:10}}>
<Picker
ref="pickAMonth"
years={[2016, 2017, 2018, 2019, 2020, 2021,2022]}
value={mvalue}
lang={pickerLang.months}
onChange={this.handleAMonthChange.bind(this)}
onDismiss={this.handleAMonthDissmis.bind(this)}
>
<MonthBox value={makeText(mvalue)} onClick={this.handleClickMonthBox.bind(this)} />
</Picker>
</div>
</div>
</form>
</div>
)
}
}
export default OldScreen;
然后我尝试了基于 React 函数的相同屏幕,它得到了这个错误,
Error: Function components cannot have refs. Did you mean to use React.forwardRef()?
尝试解决此操作是,1. 因为我使用的是 16.12.0,所以我添加了import React, { useState, useRef } from 'react';
,但添加后我也收到错误TypeError: pickAMonth.show is not a function
下面的错误程序,
import React, { useState, useRef } from 'react';
import Picker from 'react-month-picker';
import 'react-month-picker/css/month-picker.css';
import MonthBox from './MonthPicker';
function NewScreen() {
const pickAMonth = useRef(null);
const pickerLang = {months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']}
const mvalue ={year: new Date().getFullYear(), month: (new Date().getMonth())}
let makeText = m => {if (m && m.year && m.month) return (pickerLang.months[m.month-1] + '. ' + m.year)
return '?'}
const handleAMonthChange=(e)=> {
}
const handleAMonthDissmis=(e)=> {
}
const handleClickMonthBox=(e)=> {
pickAMonth.show()
}
return (
<div style={{ padding:15}}>
<form>
<div className="form-row" style={{ paddingTop:20}}>
<div className="edit" style={{width:100,paddingLeft:10}}>
<Picker
ref={pickAMonth}
years={[2016, 2017, 2018, 2019, 2020, 2021,2022]}
value={mvalue}
lang={pickerLang.months}
onChange={handleAMonthChange}
onDismiss={handleAMonthDissmis}
>
<MonthBox value={makeText(mvalue)} onClick={handleClickMonthBox} />
</Picker>
</div>
</div>
</form>
</div>
);
}
export default NewScreen;
在此先感谢您的时间!!
解决方案
要在反应中使用 ref,您需要在组件中将回调传递给 ref
不
ref="pickAMonth"
// or
ref={pickAMonth}
做
ref = {(componentRef) => refVariable = componentRef
}
ref = {ref => pickAMonth = ref}
// somwhere in your code, now you can do this
pickAMonth.show();
推荐阅读
- python - Python3 Tarfile: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf0 in position
- javascript - 从 mapbox 中检索没有地图的地理编码器的值
- java - 如何让程序检查数组 arr 的每个元素是否是同一数组的其他元素的倍数?
- dom - Embarcadero C++ 10.1 x64 中的 Try-catch 问题
- ios - *当前*正在触摸哪个 SwiftUI 节点?
- javascript - 为什么用`console.log`调用对象时会记录`[object Object]`?
- node.js - 猫鼬匹配值和更新查询
- c - 字符串指针 == NULL?
- google-cloud-platform - 返回 webhook 响应后,有没有办法在 Cloud Functions 中运行函数?
- php - 使用ajax将数据发送到codeigniter控制器