GithubHelp home page GithubHelp logo

epaew / react-liff Goto Github PK

View Code? Open in Web Editor NEW
34.0 6.0 8.0 8.02 MB

A react context provider for LIFF (LINE Front-end Framework)

License: MIT License

TypeScript 97.22% JavaScript 2.78%
liff react react-component typescript line

react-liff's Introduction

react-liff

npm version Build Status Maintainability

A react context provider for LIFF (LINE Front-end Framework)

Requirements

  • React v18.0 or later
    • React Native is not supported.
  • LIFF SDK version >=2.19.1

Getting started

When you use NPM version of LIFF SDK (Recommended)

  1. Create your React application development environment.

    • e.g. npx create-react-app app-name
  2. Add react-liff to your app dependencies.

    npm i --save @line/liff react-liff
    # or
    yarn add @line/liff react-liff
  3. Import react-liff to your app and use it!

    • An example of src/App.js

      import React, { useEffect, useState } from 'react';
      import { useLiff } from 'react-liff';
      
      import './App.css';
      
      const App = () => {
        const [displayName, setDisplayName] = useState('');
        const { error, isLoggedIn, isReady, liff } = useLiff();
      
        useEffect(() => {
          if (!isLoggedIn) return;
      
          (async () => {
            const profile = await liff.getProfile();
            setDisplayName(profile.displayName);
          })();
        }, [liff, isLoggedIn]);
      
        const showDisplayName = () => {
          if (error) return <p>Something is wrong.</p>;
          if (!isReady) return <p>Loading...</p>;
      
          if (!isLoggedIn) {
            return (
              <button className="App-button" onClick={liff.login}>
                Login
              </button>
            );
          }
          return (
            <>
              <p>Welcome to the react-liff demo app, {displayName}!</p>
              <button className="App-button" onClick={liff.logout}>
                Logout
              </button>
            </>
          );
        };
      
        return (
          <div className="App">
            <header className="App-header">{showDisplayName()}</header>
          </div>
        );
      };
      
      export default App;
    • An example of src/index.js

      import React from 'react';
      import ReactDOM from 'react-dom';
      import { LiffProvider } from 'react-liff';
      
      import './index.css';
      import App from './App';
      
      const liffId = process.env.REACT_APP_LINE_LIFF_ID;
      
      ReactDOM.render(
        <React.StrictMode>
          <LiffProvider liffId={liffId}>
            <App />
          </LiffProvider>
        </React.StrictMode>,
        document.getElementById('root')
      );

When you use CDN version of LIFF SDK

  1. Create your React application development environment.
    • e.g. npx create-react-app app-name
  2. Add react-liff to your app dependencies.
    npm i --save react-liff
    # or
    yarn add react-liff
  3. Update index.html to load LIFF SDK
  4. Import react-liff to your app and use it!

API

LiffProvider props

  • liffId: string, required
    • The ID of your LIFF application.
  • withLoginOnExternalBrowser: boolean, optional
  • plugins: Array<LiffPlugin | [LiffPlugin, LiffPluginOption]>, optional
    • List of LIFF plugin instances.
    • If you need to pass option to plugin, you can use the list of tuple [pluginInstance, pluginOption].
  • callback: (liff: Liff) => Promise<void>, optional
    • Callback function that fires after liff.init() has been succeeded.

LiffConsumer / useLiff return values

  • error: unknown (is LiffError | undefined in many cases)
    • Returns an error if liff.init() was failed.
  • isLoggedIn: boolean
    • Returns whether the user is logged in.
  • isReady: boolean
    • Returns true after liff.init() has successfully completed. Otherwise, returns false.
  • liff: Liff
    • Returns liff object.

CHANGELOG

CHANGELOG

LICENSE

MIT

react-liff's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar epaew avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

react-liff's Issues

TypeError: Cannot read properties of undefined (reading 'init')

Hi,
Thanks for the great package! The stubbing, and ease of use seem really interesting!! :)

I encounter an issue while following the README.md instruction.

Here is a repository to demo the issue with a blank create-react-app and the react-liff demo code.

The issue

Logging the Error object UI
image image

Insights

It seems to be the import of @line/liff is the issue:
image

The react-liff package is fully working when I change this line:
const liff = (_b = window.liff) !== null && _b !== void 0 ? _b : (yield Promise.resolve().then(() => __importStar(require('@line/liff')))).liff;
into
const liff = (_b = window.liff) !== null && _b !== void 0 ? _b : (yield Promise.resolve().then(() => __importStar(require('@line/liff'))));

As reference to the package source code, it corresponds to changing:
const liff = window.liff ?? (await import('@line/liff')).liff;
into
const liff = window.liff ?? (await import('@line/liff'));

(Context.tsx, l37)

I also noticed that the type declarations provided by the @line/liff package are somehow misleading as they enable both import { liff } from ... and import * as liff from ... from type checking while only the latter seems to work.

Please let me know if you have an idea of the solution, or if the suggested changes make sense to you :)

Many thanks for your support :)

Version 2.0

  • Features
    • support for LIFF plugin
  • Breaking changes
    • use official @line/liff-mock instead of my liff-stub
    • drop support for LIFF on CDN
    • drop support for React v16, v17
  • Bugfixs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.