Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React$Element<any> or React.Element<*> #4130

Closed
steida opened this issue Jun 8, 2017 · 4 comments
Closed

React$Element<any> or React.Element<*> #4130

steida opened this issue Jun 8, 2017 · 4 comments

Comments

@steida
Copy link

steida commented Jun 8, 2017

I would love to know final official recommendation how we should type element. Thank you.

@0rvar
Copy link

0rvar commented Jun 14, 2017

any allows any type, * tries to infer a single type statically -- so it depends on what you need.
If you need the variable to hold a react element regardless of props, use any. If you need to be sure the props are always the same, use *.

@dmnd
Copy link
Contributor

dmnd commented Jun 19, 2017

I'd love to see this documented too. How to type a React Element is conspicuously missing from the Flow + React page in the docs.

I've seen React$Element<*>, React.Element<*>

@youngzhao-xyz
Copy link

youngzhao-xyz commented Aug 23, 2017

@dmnd:

The updated doc now explains the difference between React$Element<*>, React.Element<*> (basically they are the same). This is based on flow v0.53.1:

import React from 'react' doesn't exposes the React types anymore. So if you want to use React.Element<*>, you need to use import * as React from 'react'.

These will all work

// I think this the best
import React from 'react';
import type { Element } from 'react';
const test: Element<*> = <div />;
import * as React from 'react';
const test: React.Element<*> = <div />;
import React from 'react';
const test: React$Element<*> = <div />; // React$Element is a global type

This doesn't work anymore

import React from 'react';
const test: React.Element<*> = <div />;

Doc: https://flow.org/en/docs/react/types/#toc-react-element
React$Element: https://github.com/facebook/flow/blob/master/lib/react.js#L159
Element: https://github.com/facebook/flow/blob/master/lib/react.js#L209

@dmnd
Copy link
Contributor

dmnd commented Aug 24, 2017

Excellent, thanks @youngzhaosignifyd. I think we can probably consider this issue closed now @steida

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants