question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why are no components extending React.component?

See original GitHub issue

Is this for performance purposes? Why not use mapDispatchToProps ?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wedneyyuricommented, Mar 5, 2017

Hi @adriaanbalt, as I said before, arrow functions aren’t the most performant solution but probaly the most readable when the components are simple (just render basic html structures). Your example isn’t wrong (my coments below).

In general, as arrow functions are just syntax sugars for React Component I use this version to develop simple components (no handlers, no logic) and I extend React.PureComponent (https://facebook.github.io/react/docs/react-api.html#react.purecomponent) to develop complex components (handlers, logic, control render lifecycles, etc…).

If you want to stick using the same syntax in the whole project I advise you to use the 2nd version.


import React from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import { provideHooks } from 'redial'
import { StyleSheet, css } from 'aphrodite'

import { initialRequest, someAction } from 'common/shell/actions'
import { getInitialData } from 'common/shell/reducer'

const redial = {
  fetch: ({ dispatch }) => {
    // doesn't seem to fire on initial load, 
    // because it was already done in server side and stored in state
    dispatch(initialRequest())
  },
  defer: ({ dispatch }) => {
    // defer is the only way to make the request on initial load due to client/index.js Line: 71
    dispatch(initialRequest())
  }
}

const mapStateToProps = state => {
  return ({
    /*
    This works great, but there is a drawback: "someData" is calculated every 
    time the component is updated. If the state tree is large, or the 
    calculation expensive, repeating the calculation on every update 
    may cause performance problems.
    See https://github.com/reactjs/reselect#motivation-for-memoized-selectors
    */    
    someData: getInitialData(state)    
  })
}
const mapDispatchToProps = dispatch => bindActionCreators({
  someAction
}, dispatch)

const App = ({ children, someData, someAction }) => {
  // WARNING: It's can be a problem because "clickButton" will have 
  // a diferent value during every render phase. 
  // See http://stackoverflow.com/questions/39260595/event-handlers-in-react-stateless-components
  const clickButton = () => someAction()

  return (
    <div className={css(styles.root)}>
      {
        someData //data from the initial requeset
        &&
        <h1>someData.title</h1>
      }
      <button onClick={clickButton}>Click</button>
      {children}
    </div>
  )
}

const styles = StyleSheet.create({
  root: {
    maxWidth: 700,
    color: '#000',
    margin: '2rem auto',
    padding: '0 1rem'
  },
})

export default provideHooks(redial)(connect(mapStateToProps, mapDispatchToProps)(App))

Read more comments on GitHub >

github_iconTop Results From Across the Web

Composition vs Inheritance - React
React has a powerful composition model, and we recommend using composition instead of inheritance to reuse code between components.
Read more >
Why we do extends React.Component when creating the ...
3 Answers 3 · So, it means that the react has provided many ways to create and render the components, either creating component...
Read more >
REACT – Simple Intro Component Not Rendering?
import mainIntro from './components' class App extends React.Component{ render(){ return( <mainIntro /> ); } } const mainNode = document.
Read more >
One small change to your React components that lets you ...
If we want to extend a component's style, then we first must make sure that the component passes down the className prop to...
Read more >
React Components - W3Schools
A class component must include the extends React.Component statement. This statement creates an inheritance to React.Component, and gives your component ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found