-
Notifications
You must be signed in to change notification settings - Fork 19
Frontend Basic Principles Separate Data Presentation
JC Quirin edited this page Jan 28, 2020
·
1 revision
When tasked with creating new functionality, consider the division of concerns between data management and the elements that will be shown on screen. Rather than having a single large component that handles everything, we want to write two different types of components:
- Stateful "container" components that handle data loading and management
- Stateless presentational components that render output based solely upon their props
// This is where we would maintain state, make calls to the API or the Redux store, etc
const MyContainerComponent = () => {
const [state, setState] = useState({foo: 'value1', bar: 'value2});
return <PresentationalComponent foo={state.foo} bar={state.bar} />
}
// Given the same props, this component will always render the same output
const MyPresentationalComponent = ({foo, bar}) => {
return (<div><h1>{foo}</h1><p>{bar}</p></div>);
}