Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - How to export a pure stateless component

People also ask

Are pure components stateless?

Notes on the definition of "pure" in React: In React, stateless components are not necessarily pure components according to the definition above if you call "stateless" a component that never calls this. setState and that doesn't use this. state .

How do I export a component in react?

Use named exports to export a component in React, e.g. export function Button() {} . The exported component can be imported by using a named import as import {Button} from './another-file. js' . You can use as many named exports as necessary in a file.

How do you make a stateless component in react?

A functional(a.k.a. stateless) component is just a plain javascript function which takes props as an argument and returns a react element. const MyStatelessComponent = props => React. createElement('div', null, props.name);

Can I export a state in react?

There are two ways to do this. The first one is to use something like redux. where you have global store for the state which can be shared across different components. import React, {Component} from 'react'; import './App.


ES6 doesn't allow export default const. You must declare the constant first then export it:

const Header = () => {
  return <pre>Header</pre>
};
export default Header;

This constraint exists to avoid writting export default a, b, c; that is forbidden: only one variable can be exported as default


Just as a side note. You could technically export default without declaring a variable first.

export default () => (
  <pre>Header</pre>
)

you can do it in two ways

const ComponentA = props => {
  return <div>{props.header}</div>;
};

export default ComponentA;

2)

export const ComponentA = props => {
  return <div>{props.header}</div>;
};

if we use default we import like this

import ComponentA from '../shared/componentA'

if we don't use default we import like this

import { ComponentA } from '../shared/componentA'

You can also use a function declaration instead of assignment:

export default function Header() {
    return <pre>Header</pre>
}

In your example, you already use curly brackets and return so this is apparently matching with your needs with no compromise.