Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor React Komposer: Execute Component Constructor AFTER ready subscription

Tags:

reactjs

meteor

I have a React Component Post:

export class Post extends React.Component {
  constructor(props) {
     this.state = this.props;
  }
  ...
}

, which I compose with

import { composeWithTracker } from 'react-komposer';

import { composeWithTracker } from 'react-komposer'; import Post from '../components/post.jsx';

function composer(props, onData) {
  const subscription = Meteor.subscribe('post', props.postId);

  if (subscription.ready()) {
    const data = {
      ready: true,
      posts: Posts.findOne(props.postId).fetch()
    }
    onData(null, data);
  } else {
    onData(null, {ready: false});
  }
}

export default composeWithTracker(composer)(Post);

. As given in the Post Component I want to put some properties in the state of the component, but the constructor will be executed before I get the data from the composer!

How do I wait until the data is send and then put my props into the state?

Isn't this what the React Kompose should do? BTW I am using Version 1.~ to get composeWithTracker.

like image 259
DrDirk Avatar asked Sep 09 '26 13:09

DrDirk


1 Answers

You could use componentWillReceiveProps to get new properties and set as component state. This function will run whenever there are new properties passed to component:

export class Post extends React.Component {
  // ...
  componentWillReceiveProps(nextProps) {
    this.setState({
      ...nextProps,
    });
  }
  // ...
}
like image 97
kkkkkkk Avatar answered Sep 12 '26 02:09

kkkkkkk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!