Toturial part II
System Architecture
Software Architecture
Write once, run anywhere
Learn once, run anywhere
Clone repo from git
change to project repo
install
Run Client ( for develope)
Build ( only for production )
$npm install
$npm run dev
$npm run build
$cd redux-startkit
https://github.com/rexhome7326/redux-startkit
Server Flow
Total Data Flow
React Library
<Router>
<Route path="/" component={App}>
{/* Show the dashboard at / */}
<IndexRoute component={Dashboard} />
<Route path="about" component={About} />
<Route path="inbox" component={Inbox}>
<Route path="messages/:id" component={Message} />
</Route>
</Route>
</Router>
Basic use
HTML5 history API
History method
this.props.history.method()
import createBrowserHistory from 'history/lib/createBrowserHistory';
const history = createBrowserHistory();
<Router history={history}>
<Route path="/" component={App}>
</Route>
</Router>
Using History
Need DOM node =>
app.use((req, res, next) => {
let history = createMemoryHistory();
let routes = pageRoutes(history);
let location = createLocation(req.url);
match({ routes, location }, (error, redirectLocation, renderProps) => {
if (redirectLocation) {
res.redirect(301, redirectLocation.pathname + redirectLocation.search)
} else if (error) {
res.status(500).send(error.message);
} else if (renderProps == null) {
res.status(404).send('Not found');
} else {
res.status(200).send(renderToString(<RouterContext {...renderProps} />))
}
});
});
Status routing
React CSS Resolution
Traditional CSS:
很好很強大..但仍有以下問題
POST-processor : just write css
Plugins :
假如我們今天要有一個button擁有四種狀態
而在不同的頁面又有不同的顏色的話,CSS該怎麼寫呢?
.button{
//button 共用屬性
&.button-special {
//button-special 共用屬性
&.normal { ... }
&.disabled { ... }
&.touched { ... }
}
&.button-normal {
//button-normal 共用屬性
&.disabled { ... }
}
}
.PageA{
.button { ... }
.button-special { ... }
.button-normal { ... }
}
.PageB{
.button { ... }
.button-special { ... }
.button-normal { ... }
}
button.scss
page.scss
.button{
//button 共用屬性
}
.normal { ... }
.disabled { ... }
.touched { ... }
button.css
//依據使用者行為切換state狀態
return(
<button styleName="button
{ this.state.buttonState }">
</button>
)
button.jsx
Using compose
.common { /* font-sizes, padding, border-radius */ }
.normal { composes: common; /* blue color, light blue background */ }
.error { composes: common; /* red color, light red background */ }
.components_submit_button__common__abc5436 { /* font-sizes, padding, border-radius */ }
.components_submit_button__normal__def6547 { /* blue color, light blue background */ }
.components_submit_button__error__1638bcd { /* red color, light red background */ }
Thinking
In
CSSModules Way
Is
REALLY HARD
Install
{
test: /\.css$/,
loader: "style-loader!css-loader?modules&localIdentName=[name]__[local]___[hash:base64:5]",
include: __dirname
}
Loader
npm install style-loader --save-dev
npm install css-loader --save-dev
npm install react-css-modules --save-dev
npm install css-module-require-hook --save-dev
Require-hook ( for isomorphic )
import CSSModules from 'react-css-modules';
export default connect(mapStateToProps, { loadDemo })(CSSModules(Demo,style));
Composed component
( using styleName syntax )
hook({
generateScopedName: '[name]__[local]___[hash:base64:5]',
});
We commend :
Using <className="name"> for Global CSS
Using <styleName="name"> for LOCAL CSS