r/reactjs • • Jun 03 '18

Beginner's Thread / Easy Question (June 2018)

Hello! just helping out /u/acemarke to post a beginner's thread for June! we had over 270 comments in last month's thread! If you didn't get a response there, please ask again here! You are guaranteed a response here!

Soo... Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! We’re a friendly bunch. No question is too simple.

The Reactiflux chat channels on Discord are another great place to ask for help as well.

Pre-empting the most common question: how to get started learning react?

You might want to look through /u/acemarke's suggested resources for learning React and his React/Redux links list. Also check out http://kcd.im/beginner-react.

29 Upvotes

537 comments sorted by

View all comments

Show parent comment

1

u/NiceOneAsshole Jun 26 '18

My preference is creating a HOC "container" component that you would use in both instances to fetch the data and pass it as props.

Using decorator syntax, it would look something like this -

@foobarDataDecorator
class Dashboard extends Component {....
        
@foobarDataDecorator
class OtherPage extends Component {...

Then within your foobarDataDecorator -

export default function foobarDecorator(DecoratedComponent) {
     @connect(...)
     class FooBarDecorator extends Component {
         componentDidMount() {
              dispatch(fetchAction())
         }
         render() {
              return (
                 <DecoratedComponent
                      {...props}
                      fooBar={this.props.fooBar}
                  />
             );
        }
       
   return foobarDecorator;
}

But about halfway through writing this, I realized this combines a few advanced patterns.

So to simplify, I think dispatch actions in each component is better than throwing them in a root component like App.js. BUT, you're free to construct your app how you see fit. That's the fun part of learning.

2

u/ramonf Jun 26 '18

Appreciate the reply! I'll keep messing with it. I'm just trying to get myself familiar with as much as possible.