Botframework-webchat: Web Chat throwing "Invalid Hook" error with React Hooks

Created on 1 Aug 2019  路  1Comment  路  Source: microsoft/BotFramework-WebChat

Screenshots

image

Version

v4.5.0 built from master branch

Description

The development team recently bumped Web Chat's React dependency to v16.8.6 which introduces React Hooks to Web Chat. While working on a pull request for the typing indicator, I tried to use the useState and useEffect hooks in a component; however, Web Chat threw an "Invalid Hook" Error. I created a really simple component to double check it wasn't my logic, but Web Chat still threw the error.

To Reproduce

Add a simple component with React Hooks to Web Chat.
```javascript
const TypingIndicator = () => {
const [item] = useState('Hello, World!');
return

{ item }

}
````

Expected behavior

React Hooks should not cause Web Chat to crash.

[Bug]

Bug

Most helpful comment

After some investigations, the core reason is, Webpack doesn't play well with npm link (via lerna), thus, multiple copies of react are being loaded. And React hooks doesn't like multiple copies of React.

  • We should not include react and react-dom in bundle as dependencies in any case (we are component package, should never bring the framework)

    • We should include react and react-dom as peer dependencies and requires the application to include them

  • We should include react in the root package. The root package contains all devDependencies which is similar to "manually hoisted packages"

    • We try not to use hoist in lerna because it conflict with Babel bridge

    • If we hoist, we will have two packages of same name, [email protected] vs. babel@6, and lerna prefer 6, which broke Jest because it use the bridge

  • We cannot include react in playground as well

    • For unknown reason, Webpack dev server in playground tried to load two copies of React



      1. Web Chat bundle/component, it load the react from root /node_modules


      2. Playground app, it load the react under /packages/playground/node_modules



    • If we rename/remove the React package at root, Webpack dev server works fine, but we shouldn't

Some package dependencies cleanup is needed to enable React hooks in a monorepo with both component and app. It's a known issue across the community.

>All comments

After some investigations, the core reason is, Webpack doesn't play well with npm link (via lerna), thus, multiple copies of react are being loaded. And React hooks doesn't like multiple copies of React.

  • We should not include react and react-dom in bundle as dependencies in any case (we are component package, should never bring the framework)

    • We should include react and react-dom as peer dependencies and requires the application to include them

  • We should include react in the root package. The root package contains all devDependencies which is similar to "manually hoisted packages"

    • We try not to use hoist in lerna because it conflict with Babel bridge

    • If we hoist, we will have two packages of same name, [email protected] vs. babel@6, and lerna prefer 6, which broke Jest because it use the bridge

  • We cannot include react in playground as well

    • For unknown reason, Webpack dev server in playground tried to load two copies of React



      1. Web Chat bundle/component, it load the react from root /node_modules


      2. Playground app, it load the react under /packages/playground/node_modules



    • If we rename/remove the React package at root, Webpack dev server works fine, but we shouldn't

Some package dependencies cleanup is needed to enable React hooks in a monorepo with both component and app. It's a known issue across the community.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stevengum picture stevengum  路  4Comments

adriantan08 picture adriantan08  路  3Comments

naveen-vijay picture naveen-vijay  路  4Comments

GewoonMaarten picture GewoonMaarten  路  3Comments

prashanthsridhar picture prashanthsridhar  路  3Comments