[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]
by Web Desk via Digital Information World
"Mr Branding" is a blog based on RSS for everything related to website branding and website design, it collects its posts from many sites in order to facilitate the updating to the latest technology.
To suggest any source, please contact me: Taha.baba@consultant.com
One common trend I find among most Redux developers is a hatred towards setState(). A lot of us (yes, I've fallen into this trap many times before) flinch at the sight of setState() and try to keep all the data in our Redux store. But, as the complexity of your application grows, this poses several challenges.
In this post, I'll walk you through various strategies to model your state, and dive into when each of them can be used.
Redux works with the principle of being the single source of truth for your application state. A new Game of Thrones season is airing now, and I'm sure everyone's excited to know how this is going to unfold. Let's build a fun Game of Thrones fan listing page, to understand these concepts in detail.
Note: I'll be using yarn to run the app. If you don't have yarn set up, replace yarn with npm.
Before we dive in, download the basic skeleton from the repo and run:
yarn install
yarn run start
You should see a basic list page with some of your favorite GoT characters listed.
Note: We'll be using the ducks pattern to write our application. It reduces unnecessary module imports and cuts down on a lot of boilerplate.
The scope of this article is to help you structure your Redux apps. It assumes a basic knowledge of the library. I'll give a brief overview of Redux concepts that will help you follow the rest of the article better. If you're familiar with how these works, feel free to skip this section.
All Redux apps make use of four important constructs: actions, reducers, a store, and containers.
An action is an intent to update the state. It could be triggered by a network call, or a user clicking a button. Actions have two parts:
For this example, we'll be using a library called redux-actions to create actions.
A reducer is a function that listens for an action and returns a new state representation.
An application can be divided into many reducers, representing various parts of the page. A store brings all these together and keeps the app state intact.
Containers connect your app state and actions with the component, passing them down as props.
To get a deep understanding of how this works, I'd encourage you to first look at the free introduction series by Dan Abramov.
The list page is nice, but the names don't give any context to people who are new to the GoT universe. Let's extend the component to render the character description as well:
//GoTCharacter.js
export const CharacterRow = ({character}) => (
<div className="row">
<div className="name">{character.name}</div>
<div className="description">{character.description}</div>
</div>
);
While this solves the problem, our designers feel that the page looks clumsy, and it's a better idea to collapse this information till users want it. There are three different approaches we can take to solve this problem.
setState approachThe simplest way to achieve this in React is using setState() to store the data within the component itself:
//GoTCharacter.js
export class StatefulCharacterRow extends Component {
constructor() {
super();
this.state = {
show_description: false
}
}
render() {
const {character} = this.props;
return (<div className="row">
<div className="name">{character.name}</div>
<a href="#" onClick={() => this.setState({
show_description: !this.state.show_description})} >
{this.state.show_description ? 'collapse' : 'expand'}
</a>
{this.state.show_description &&
<div className="description">{character.description}</div>}
</div>);
}
};
Using setState() is fine as long as the state we're dealing with is only local to the component. If, for instance, we want to put in place an "expand all" function, it will be difficult to handle this with just React.
Let's see how we can move this to Redux:
// FlickDuck.js
// …
export const toggleCharacterDescription = createAction(
FlixActions.TOGGLE_CHARACTER_DESCRIPTION, (character) => ({character})
);
export default (current_state, action) => {
const state = current_state || default_state;
switch (action.type) {
case FlixActions.TOGGLE_CHARACTER_DESCRIPTION:
return {...state, characters: state.characters.map(char => {
if (char.id === action.payload.character.id) {
return {...char,show_description: !char.show_description};
}
return char;
})}
default:
return state
}
}
// GoTCharactersContainer.js
import { connect } from 'react-redux';
import GoTCharacters from './GoTCharacters';
import {toggleCharacterDescription} from './FlickDuck';
const mapStateToProps = (state) => ({
...state.flick
});
const mapDispatchToProps = (dispatch) => ({
toggleCharacterDescription : (data) => dispatch(toggleCharacterDescription(data))
});
export default connect(mapStateToProps, mapDispatchToProps)(GoTCharacters);
// GoTCharacters.js
const GoTCharacters = ({characters,toggleCharacterDescription}) => {
return (
<div className="characters-list">
{characters.map(char => (
<CharacterRow
character={char}
toggleCharacterDescription={toggleCharacterDescription}
key={char.id}/>
))}
</div>
);
};
export const CharacterRow = ({character, toggleCharacterDescription}) => (
<div className="row">
<div className="name">{character.name}</div>
<a href="#" onClick={toggleCharacterDescription.bind(null, character)} >
{character.show_description ? 'collapse' : 'expand'}
</a>
{character.show_description &&
<div className="description">{character.description}</div>}
</div>
);
We're storing the state of the description field inside the character object. Our state will look like this now:
state = {
characters: [{
id: 1,
name: "Eddard Ned Stark",
house: "stark",
description: "Lord of Winterfell - Warden of the North - Hand of the King - Married to Catelyn (Tully) Stark",
imageSuffix: "eddard-stark",
wikiSuffix: "Eddard_Stark",
show_description: true
},
{
id: 2,
name: "Benjen Stark",
house: "stark",
description: "Brother of Eddard Stark - First ranger of the Night's Watch",
imageSuffix: "benjen-stark",
wikiSuffix: "Benjen_Stark",
show_description: false
}]
}
This is a general pattern a lot of developers follow when they're starting out with Redux. There's nothing wrong with this approach, and it works great for smaller apps.
So far, we've been dealing with the characters from the first chapter of GoT, and the universe is about to get a whole lot bigger. When it does, our app will become slow. Imagine looping through 1000 characters to update one row.
Let's see how to scale this for a larger dataset:
// FlickDuck.js
// …
case FlixActions.TOGGLE_CHARACTER_DESCRIPTION:
const {character} = action.payload;
return {
...state,
character_show_description: {
...state.character_show_description,
[character.id]: !state.character_show_description[character.id]
}
}
// …
And in GoTCharacters.js:
export const CharacterRow = ({character, character_show_description, toggleCharacterDescription}) => (
<div className="row">
<div className="name">{character.name}</div>
<a href="#" onClick={toggleCharacterDescription.bind(null, character)} >
{character_show_description[character.id] ? 'collapse' : 'expand'}
</a>
{character_show_description[character.id] &&
<div className="description">{character.description}</div>}
</div>
);
When the user clicks on the expand link, we update the character_show_description with the current character id. The state looks like this now:
state = {
characters: [...],
character_show_description: {
1: true,
2: false
}
}
Now we can update the UI state without looping over all the characters.
Continue reading %To Redux or Not: the Art of Structuring State in React Apps%
Engaging and impressive One Pager investigating the famous 2003 student murders in Sydney. The long scrolling site gives the unique perspective of two people with interactive content switchers. The research is thorough and the design is beautiful featuring tons of gritty textures and subtle animations as you scroll. This is the fifth Most Loved award in a row now for SBS. Respect
Whether you’re starting a new project or your existing blog has moved beyond the WordPress.com or Medium world, at some point you’re going to want to dive into the world of hosting. This is a one-stop shop that’ll give you the information you need to get started. Whether you’re deciding whether to take the hosting […]
Continue reading %Website Hosting: Everything You Need to Get Started%
So you've decided to run a WordPress site but have no idea where to start? This tutorial is aimed at absolute beginners. Some IT knowledge will help but I presume you want to learn the essentials within a few hours. Let's get started.
A little planning goes a long way. Be honest with yourself: why are you considering WordPress? Do you want to:
WordPress is flexible and runs almost a third of the web — but it's not ideal for every situation. A website or article library is perfect. Creating a social network or online shop is possible but there may be better options. Using WordPress to learn PHP could be a frustrating experience.
Presuming WordPress is appropriate, are you interested in the technicalities or would you simply prefer to write content? If it's the latter, a managed WordPress plan from SiteGround or an account at WordPress.com will get you running without the hassles of installation and server management.
The moral: define the problem before choosing a solution!
Ideally, you should have all your content written before building a site. It's the best way to plan a structure and will influence your design. No one ever does that, but at least plan a few general concepts so you have somewhere to start.
A domain name is your primary web address, e.g. www.mysite.com. Keep it short and use keywords appropriate to your content. This can be tougher than it sounds; most good names were registered years ago.
Use a reputable domain registrar. Prices vary across countries and top-level-domain types (.com, .net, .org, .ninja etc), but expect to pay around $25 for a new domain for a couple of years. Buying a decent pre-registered domain from someone else can be considerably more expensive.
Your site needs to be hosted somewhere. Its files must be placed on a device which understands how to deal with web requests: a web server. You could serve everything from your desktop PC but it quickly becomes impractical.
Buy a suitable plan from a respected host such as SiteGround. A WordPress-compatible shared hosting plan costs a few dollars a month and you can upgrade disk space and bandwidth as traffic grows.
You will then need to 'point' your domain at your new web space. This is normally done by logging into your domain registrar's control panel then either:
All hosts and domain registrars provide guidance but you may need to seek expert assistance. Domain changes can take up to 48 hours to propagate so you may need to wait before moving to the next step.
Secure Socket Layer (SSL) certificates enable cryptographic protocols on your website so it is served over an https:// address rather than http://. All communication between your server and the user's browser is encrypted so it cannot be (easily) intercepted by a third party.
Configuring SSL is an optional step but highly recommended:
Hosts often allow you to install a certificate purchased elsewhere, but it's easier to use their own service. For example, SiteGround provides a free Let's Encrypt option in the security section of your site's cPanel. Click that, hit Install and SSL is enabled.
WordPress is a complex application which requires:
The majority of hosts provide cPanel - a popular website management facility. You can create your database, upload WordPress and install manually. For full instructions, refer to How to Create WordPress MySQL Databases on cPanel.
Fortunately, there is an easier option. Search or browse for the WordPress options in cPanel:
Click the WordPress Installer to open the installation panel:
Define the following settings:
The other options can normally be left as the default settings. Hit Install and wait a few minutes for the installation process to complete. You will be given a link to the main site (https://mysite.com/) and the WordPress control panel (http://ift.tt/1VgZ4m9) where you can log in with your administrative username and password.
Don't be tempted to start publishing content just yet! It's best to configure WordPress from the Settings menu before going further:
The following sections describe the basic WordPress settings but note that installed themes and plugins can override these options.
This pane allows you to change various aspects about your installation. The primary settings to change include:
Remember to hit Save Changes once finished.
The main settings to change in this pane are:
The Front page displays setting allows you to set whether your latest posts or a static page is presented on the home page.
The other default settings are normally fine, although you may want to temporarily disable Search Engine Visibility during the initial stages of building your site. Don't forget to enable it before going live!
This pane controls commenting. The main setting is Allow people to post comments on new articles which you may want to disable if you don't require comments.
Continue reading %The Ultimate Beginner’s Guide to Setting Up & Running a WordPress Site%
Long-scrolling Landing Page with trendy gradients and drop-shadowed elements promoting CanvasFlip Live – a service that livestreams user interactions with your prototype.
|