Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #27: resolves existing layout redundancy.
NOTE: this was branched out of #26, as of its creation still not merged, because it requires the webpack changes from there.
Overview
Any piece of layout code that can potentially be shared between pages, or even repeated inside the same page, should go in its own
BesugoComponent
in a.jsx
file. This way, we won't have to copy-paste repeating layouts in every file, or even rewrite it for CMS previews.BesugoComponent
A Class extension of
React.Component
, what matters most to us is its ability to parse JSX from any data supplied to it, and that it can reuse other components when rendering itself.All such components should have the following basic structure to be built:
Below is a structural overview of a BesugoComponent extended class.
constructor
Only mandatory if the component expects to be passed any props (i.e. data from attributes in html). Should always take the form shown above.
config
A static object that defines this component within our website.
/admin
Example:
getData()
An optional method to fetch and return whatever data is needed to build the component. If in a CMS preview page, it should fetch the data from the methods supplied by the
CMS
global object; otherwise it would fetch from its props object.Use
this.isPreview()
to check whether we're in a CMS preview content.Example:
In most cases, you would pass any necessary data as attributes of that placeholder, and they will be direct properties of the
this.props
object:resolves automatically to
If there's a need, you can pass more complicated data from hugo templates as children of the placeholder element, or as a JSON text inside it. The placeholder DOM object is given to the component as
this.props.xplaceholder
, so you can pass the data however you want as long as you fetch it appropriately.Example hugo template:
fetch in the component as:
renderBlock()
Where the actual layout for this component goes; must return a single JSX object, so if necessary you must encapsulate the whole output in an outer
div
container or equivalent.React elements have some syntax differences, the most important being:
class
attributes should be defined asclassName
instead;style
attributes expect a JS style object rather than a text string:style={ { background: 'red' } }
Example:
Although it's not necessary, when the component will only be used for direct output of a block and not for any content preview, you can define the layout in the
render()
method instead as you would in a typical React component.You can also include other components within another component's layouts; don't forget to pass any data necessary to build it:
renderPreview()
This is the layout for the preview area of this component. Typically, you will encapsulate the above
renderBlock()
within another JSX layout: