转换为钩子

时间:2021-04-07 12:11:37

标签: react-native hook i18next

我仍在使用 react native hooks,我需要一些转换 const 的帮助。

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, { 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>
    )
}

1 个答案:

答案 0 :(得分:1)

这应该是正确的:

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>
 )
}