React native 转换为挂钩
我仍然在使用react原生钩子,我想得到一些转换常量的帮助React native 转换为挂钩,react-native,hook,i18next,React Native,Hook,I18next,我仍然在使用react原生钩子,我想得到一些转换常量的帮助 import React, { Component } from 'react'; export default class Booking extends React.Component { myTitle = () => { const { people } = this.props; const nameFirst = idx(people, __ => __.na
import React, { Component } from 'react';
export default class Booking extends React.Component {
myTitle = () => {
const { people } = this.props;
const nameFirst = idx(people, __ => __.nameFirst);
return nameFirst ? i18n.t('Generate.NameOk', {nameFirst}) : i18n.t('Generate.NameNull');
};
render() {
return(
<View>
{this.myTitle()}
</View>
)
}
}
import React,{Component}来自'React';
导出默认类。组件{
myTitle=()=>{
const{people}=this.props;
const nameFirst=idx(people,_uu=>uuuuuuu.nameFirst);
返回nameFirst?i18n.t('Generate.NameOk',{nameFirst}):i18n.t('Generate.nameFirst');
};
render(){
返回(
{this.myTitle()}
)
}
}
我这样做是对的
import React, { useState } from 'react';
import { useTranslation } from 'i18next';
function Booking() {
const { t } = useTranslation();
const myTitle = () => {
const [ people, setPeople ] = useState();
const nameFirst = idx(people, __ => __.nameFirst);
return nameFirst ? t('Generate.NameOk', {nameFirst}) : t('Generate.NameNull');
};
return(
<View>
{myTitle()}
</View>
)
}
import React,{useState}来自“React”;
从“i18next”导入{UseTransation};
功能预订(){
const{t}=useTranslation();
常量myTitle=()=>{
const[people,setPeople]=useState();
const nameFirst=idx(people,_uu=>uuuuuuu.nameFirst);
返回nameFirst?t('Generate.NameOk',{nameFirst}):t('Generate.nameFirst');
};
返回(
{myTitle()}
)
}
这应该是正确的:
export const Booking = () => {
const { t } = useTranslation();
const [ people, setPeople ] = useState();
const myTitle = () => {
const nameFirst = idx(people, __ => __.nameFirst);
return nameFirst ? t('Generate.NameOk', {nameFirst}) : t('Generate.NameNull');
};
return(
<View>
{myTitle()}
</View>
)
}
export const Booking=()=>{
const{t}=useTranslation();
const[people,setPeople]=useState();
常量myTitle=()=>{
const nameFirst=idx(people,_uu=>uuuuuuu.nameFirst);
返回nameFirst?t('Generate.NameOk',{nameFirst}):t('Generate.nameFirst');
};
返回(
{myTitle()}
)
}
为什么要使用函数而不是常量预订=()=>?我认为钩子应该只在组件中使用,这里将是一个组件,因为它返回一些jsx。