일기장 오늘의 App.ts는 고급 기술을 연습해보았다

import { Component, createElement } from 'react';
import { Text } from 'react-native';
import { DocumentDirectoryPath, readFile, writeFile } from 'react-native-fs';
import _random from 'lodash/random';

 

class FileIO {
    protected path = DocumentDirectoryPath + '/../Desktop/';

 

    constructor(file_name: string) {
        this.path += file_name;
    }

 

    public read() {
        return readFile(this.path)
            .then(content => {
                return content;
            })
            .catch(e => {
                throw new e.code + '\n' + e.message;
            });
    }

 

    public write(file_content: string) {
        writeFile(this.path, file_content)
            .catch(e => {
                throw new e.code + '\n' + e.message;
            });
    }
}

 

export default class App extends Component<undefined, { str: string }> {
    constructor(props: any) {
        super(props);
        this.state = { str: '' };
    }

 

    protected io = new FileIO('test.txt');

 

    async componentDidMount() {
        this.io.write(_random(9).toString());
        const file_str = await this.io.read();
        this.setState({ str: file_str });
    }

 

    render() {
        return createElement(Text, undefined, this.state.str);
    }
}

댓글 6

리눅스 작성자 2022.04.13. 21:33
 ♥무케♥
_.random이 _random 된 거 말하는 거면
npm 패키지 중에 babel-plugin-lodash에 있는 기능임
댓글
권한이 없습니다. 로그인

신고

"님의 댓글"

이 댓글을 신고 하시겠습니까?

삭제

"님의 댓글"

이 댓글을 삭제하시겠습니까?

분류 제목 글쓴이 날짜 조회 수
공지 [공지] 츄르내역_240102 19 조유리 3076 12
공지 [공지] 공지사항_220412 30 조유리 6539 64
일기장
이미지
Sso! 31 1
일기장
기본
메르시장인 51 7
일기장
기본
메르시장인 60 7
일기장
기본
메르시장인 82 14
일기장
기본
메르시장인 59 9
일기장
기본
메르시장인 55 5
일기장
기본
메르시장인 41 5
일기장
기본
메르시장인 44 8
일기장
기본
메르시장인 69 11
일기장
기본
메르시장인 64 8
일기장
기본
철이두개면두철 28 3
일기장
기본
메르시장인 46 8
일기장
이미지
♥무케♥ 35 3
일기장
이미지
구내염에는알보칠 158 5
일기장
기본
메르시장인 63 9
일기장
기본
메르시장인 58 8
일기장
이미지
정진솔 31 3
일기장
이미지
정진솔 64 7
일기장
기본
육상천재방예나 62 5
일기장
기본
메르시장인 80 10