S. Schenk S. Schenk - 1 year ago 96
Javascript Question

How can I Initialize the state when using a stateless function to define my component

I would like to use a stateless function to define my component, but that means I can't use life cycle methods like componentWillMount. So how can I render the component with initial content?

example of the above:

export const Panel = () => (<div></div>)

Answer Source

You can use default props like this:

export const Panel = ({name = 'Default user', age = 'N/A'}) => (
  <div>{name} {age}</div>

More about destructuring objects

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download