Showing posts with label JSX. Show all posts
Showing posts with label JSX. Show all posts

ReactJS : Bootstrap Thumbnail Matrix / Grid Gallery




Demo code: Download


for running :]

<script type="text/babel">

'use strict';

var ThumbnailMatrixItem= React.createClass({
render: function() {
return (
 <div className="col-lg-3 col-md-4 col-xs-6">
          <a href="#" className="d-block mb-4 h-100">
            <img className="img-fluid img-thumbnail" src={this.props.imgUrl} 
alt="" />
          </a>
        </div>
 );
 }
});

var ThumbnailMatrix= React.createClass({
render: function() {
var productNodes= this.props.items.map(function (product) {
 return (
  <ThumbnailMatrixItem name={product.name} description={
product.description} price={product.price} imgUrl={product.imgUrl}>
</ThumbnailMatrixItem>
        );
 });

return (
 <div className="container"> 
       <h1 className="my-4 text-center text-lg-left">{this.props.title}</h1>

        <div className="row text-center text-lg-left">
 {productNodes}
 </div>
 </div>
       );
}
});

ReactDOM.render(<ThumbnailMatrix title="Bootstrap Thumbnail Gallery" items={
productData}/>, document.getElementById('thumbnailGridDemo'));



//product data

var productData= [
{name: "Priduct 1", description: "product1 description goes here",imgUrl:
 "img/400x300.png", price: "$150"},
{name: "Product 2", description: "product2 description goes here",imgUrl:
 "img/400x300.png",price: "$50"},
{name: "Product 3", description: "product3 description goes here",imgUrl:
 "img/400x300.png",price: "$30"},
{name: "Priduct 41", description: "product1 description goes here",imgUrl:
 "img/400x300.png",price: "$150"},
{name: "Product 52", description: "product2 description goes here",imgUrl:
 "img/400x300.png",price: "$50"},
{name: "Product 63", description: "product3 description goes here",imgUrl:

 "img/400x300.png",price: "$30"},
{name: "Priduct 71", description: "product1 description goes here",imgUrl: 
"img/400x300.png",price: "$150"},
{name: "Product 82", description: "product2 description goes here",imgUrl: 
"img/400x300.png",price: "$50"},
{name: "Product 93", description: "product3 description goes here",imgUrl: 
"img/400x300.png",price: "$30"},
{name: "Product 95", description: "product3 description goes here",imgUrl:
 "img/400x300.png",price: "$30"},
{name: "Product 96", description: "product3 description goes here",imgUrl: 
"img/400x300.png",price: "$30"},
{name: "Product 99", description: "product3 description goes here",imgUrl: 
"img/400x300.png",price: "$30"},
 ];
</script>

Dependencies:

    <script src="libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="libs/bootstrap/4.1.1/js/bootstrap.bundle.min.js"></script>
    <script src="libs/react/0.14.2/react.js"></script>
    <script src="libs/react/0.14.2/react-dom.js"></script>
    <script src="libs/babel/babel-core/5.8.23/browser.min.js"></script>


Getting started with Preact : helloworld standalone example

Preact looks to be the most closest React alternative in terms of code similarity. There official motto: "Fast 3kB alternative to React with the same ES6 API". You almost have to change nothing in the codes if you look to switch your React application to Preact. Lets have a quick look at a simplest Preact standalone helloworld component example with JSX/babel embedded in single html page, the very 'clock' example in their documentation :

  1. <html>
  2. <head></head>
  3. <body>
  4. <div id="abcd"></div>
  5. <script src="https://npmcdn.com/preact@2.8.3/dist/preact.js"></script>
  6. <script src="https://npmcdn.com/preact-compat@0.6.1/preact-compat.js"></script>
  7. <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.min.js"></script>
  8. <script>
  9. window.React = preactCompat;
  10. window.ReactDOM = preactCompat;
  11. </script>
  12. <script type="text/babel">
  13. const { h, render, Component } = preact; // normally this would be an import statement.
  14. class Clock extends Component {
  15. constructor() {
  16. super();
  17. // set initial time:
  18. this.state.time = Date.now();
  19. }
  20. componentDidMount() {
  21. // update time every second
  22. this.timer = setInterval(() => {
  23. this.setState({ time: Date.now() });
  24. }, 1000);
  25. }
  26. componentWillUnmount() {
  27. // stop when not renderable
  28. clearInterval(this.timer);
  29. }
  30. render(props, state) {
  31. let time = new Date(state.time).toLocaleTimeString();
  32. return <span>{ time }</span>;
  33. }
  34. }
  35. render(<Clock/>, document.getElementById('abcd'));
  36. </script>
  37. </body>
  38. </html>

Reference:

react : update parent state from within child

  1. var Child = React.createClass({
  2. handleClick: function(event){
  3. if (confirm("Press a button!")) {
  4. this.props.sendChildConfirm();
  5. } else {
  6. this.props.sendChildCancel();
  7. }
  8. }
  9. ,
  10. render: function () {
  11. return (
  12. <a style={{border: 1}} onClick={this.handleClick}>update parent state from within child</a>
  13. );
  14. },
  15. });
  16. var Parent = React.createClass({
  17. getInitialState: function() {
  18. return {childText: 'Nothing'};
  19. },
  20. render: function () {
  21. return (
  22. <div>
  23. <h3>Child says {this.state.childText}</h3>
  24. <Child sendChildConfirm={this.handleConfirm} sendChildCancel={this.handleCancel} />
  25. </div>
  26. );
  27. },
  28. handleConfirm: function(event) {
  29. this.setState({childText: 'Yes!'});
  30. },
  31. handleCancel: function(event) {
  32. this.setState({childText: 'No!'});
  33. }
  34. });
  35. ReactDOM.render(
  36. <Parent />,
  37. document.getElementById('react-child-parent-dom-node')
  38. );

Download full working code : Download

React : Bootstrap List/Grid Toggle





Demo code Download .

Please make sure running this demo with firefox locally.
With chrome you may get this error message with default configuration:

XMLHttpRequest cannot load file:///.../js/app.js. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file://...//js/app.js'.

With chrome try using the --disable-web-security command line param to make it work.

References:
https://facebook.github.io/react/docs/tutorial.html
https://github.com/facebook/react
http://www.programming-free.com/2013/12/fading-list-grid-view-switch-jquery.html
https://github.com/facebook/react/tree/master/examples/jquery-bootstrap

ReactJS : Bootstrap List Example














Demo Code Download .

Javascript/JSX(app.js):

  1. 'use strict';
  2. // lets populate list with this JSON array dynamically
  3. var data = [
  4. {author: "Pete Hunt", text: "This is one comment"},
  5. {author: "Jordan Walke", text: "This is *another* comment"},
  6. {author: "Pete Hunt", text: "This is second comment"}
  7. ];
  8. var Comment = React.createClass({
  9. render: function() {
  10. return (
  11. <li className="list-group-item">
  12. <b>{this.props.text}</b> : by - <span className="author-look">{this.props.author}</span>
  13. </li>
  14. );
  15. }
  16. });
  17. var CommentList = React.createClass({
  18. render: function() {
  19. var commentNodes = this.props.data.map(function (comment) {
  20. return (
  21. <Comment author={comment.author} text={comment.text}></Comment>
  22. );
  23. });
  24. return (
  25. <ul className="list-group list-look">
  26. {commentNodes}
  27. </ul>
  28. );
  29. }
  30. });
  31. ReactDOM.render(<CommentList data={data}/>, document.getElementById('myListDiv'));

Simplest index.html to get us started :)

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6. <title>jQuery Integration</title>
  7. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" type="text/css" />
  8. <link rel="stylesheet" href="css/example.css" type="text/css" />
  9. </head>
  10. <body>
  11. <div id="myListDiv"></div>
  12. <script src="https://cdn.jsdelivr.net/react/0.14.0-rc1/react.min.js"></script>
  13. <script src="https://cdn.jsdelivr.net/react/0.14.0-rc1/react-dom.min.js"></script>
  14. <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.min.js"></script>
  15. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js" charset="utf-8"></script>
  16. <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js" charset="utf-8"></script>
  17. <script type="text/babel" src="js/app.js"></script>
  18. </body>
  19. </html>




References:
https://facebook.github.io/react/docs/tutorial.html#hook-up-the-data-model