Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function or variable outside of a class in react

I was exploring react, and I saw this piece of code.

const renderLine = (user, key) => <li key={key}><b>{key}</b>: {user[key]}</li>

export default class App extends Component {
  ...
  ...
  render () {
    return (
      <div className='App'>
          {Object.keys(user).map(key => renderLine(user, key))}
      </div>
    )
  }
}

Why the renderLine is outside of the App class? What's the intention of this pattern? Usually I will just put inside a class and use it like this.renderLine()

like image 307
Casa Lim Avatar asked Mar 26 '18 03:03

Casa Lim


People also ask

Is it better to use function or class in React?

React recommends that Functional Components are used over classes, and even will throw warnings about a few Class Component lifecycle methods that are being deprecated.

Can a function use variable from outside?

variables declared inside a function can only be accessed inside that function. think of each function as a box and the lid can only be opened from inside: the function can grab what's outside or put something out, but you can't reach in its box. you'd have to ask for it politely first.

Can I use both class and function in React?

It's just a function which accepts props and returns a React element. But you can also use the ES6 class syntax to write components. Both versions are equivalent and will give you the exact same output.


3 Answers

You can put it in the class, for sure, without any problem. Exposing it outside the class makes it a helper function that can be used outside the class/component's scope.

You can even organise these kind of code into a helper function outside this js file, e.g. UIHelper.js:

//UIHelper.js
const renderLine = (user, key) => <li key={key}><b>{key}</b>: {user[key]}

const UIHelper = {
    renderLabel: renderLabel,        //Other helper functions
    renderLine: renderLine,          //<----------Export this
    renderDateTime: renderDateTime,  //Other helper functions
    ...
}
export default UIHelper;

Usage:

//Other.js
import UIHelper from '../what/ever/path/UIHelper'

render (){
...
   {UIHelper.renderLine()}
...
}
like image 164
David Avatar answered Oct 27 '22 01:10

David


Reusable is the answer. Putting it in your class and you can't use it in another component.

In React there are two main type of components: stateful and stateless. You use stateless components to render small things that don't own its data. renderLine is the perfect example: it just render whatever you give it, in a line.

like image 32
Huy Vo Avatar answered Oct 27 '22 01:10

Huy Vo


A function is same as a stateless component.
However it's less common to write it this way, since the components inside mapping function require key, but not every row need to display the key value.

const renderLine = (user, key) => (<li key={key}><b>{key}</b>:{user}</li>);

const RenderStatelessLine = props => (<li>{props.user}</li>);

export default class App extends Component {
  render () {
    return (
      <div className='App'>
          {Object.keys(users).map(key => renderLine(users[key], key))}
          {Object.keys(users).map(key => <RenderStatelessLine user={users[key]} key={key} />)}
      </div>
    )
  }
}
like image 32
FisNaN Avatar answered Oct 27 '22 00:10

FisNaN