8/7/16

React - Building Search App (Part 4)

Events

So, in previous part we learned about props and state, and our search app already able to display some results.
The results are came from parent SearchPage component, passed to ResultsList child component which displays them.

But what about SearchBar component? Isnt it suppose to play the key role in the app? Well, indeed SearchBar suppose to pass user input when somebody search for something and typing-in some chars. SearchBar should speak to SearchPage and it should perform search and pass the results to ResultsList

Handle Text Change

For to catch user input of search input box you must specify handler in the onChange attribute:


    var SearchBar = React.createClass({
     
      onChange: function(e) {
        console.log(e.target.value);
      }, 
      render: function() {
        return (
          <div><input placeholder="type search term" onChange={this.onChange}/></div>
        );
      }
    }); 
Now you can see the console prints the chars user enters. Yo ho!@!@!

Pass the input to the parent component

Lets take an advantage of the technique we just learned in previous part - lets use the props. But how? We can modify state but we should avoid from trying to update the props from inside the component!
Actually we not have to modify things for passing data - instead of placing some usual javascript variable into attribute (prop) as we did with ResultsList component, we can set the prop with the function!


    //Search Page
    var SearchPage = React.createClass({
      getInitialState: function() {
        return {results: [{text:'some result'}]};
      }, 
      handleSearchTextChange: function(txt){
        console.log(txt)
      },
      render: function() {
        return (
          <div>
            <SearchBar onTextChange={this.handleSearchTextChange}/>//<---here is the magic!
            <ResultsList results={this.state.results}  />
          </div>
        );
      }
    });
    
Now the only thing left is to modify SearchBar the way it will use the passed function :

    var SearchBar = React.createClass({
     
      onChange: function(e) {
        this.props.onTextChange(e.target.value);
      }, 
      ...
    }); 
Now we just make SearchBar to be able to speak to its parent SearchPage!
see plnkr
If you find this stuff interesting - continue to the next post

8/3/16

React - Building Search App (Part 3)

Props(Properties)

Props are the attributes which can be accessible from inside the react component.
For our example we can define the results list array and put it in the results property (attribute) of ResultsList component.
First step: lets modifiy ResultsList components so it will able to display array of results:



    // Results List
    var ResultsList = React.createClass({
      //this.props.results should be set with array of search results 
      render: function() {        
          var createItem = function(item, idx) {
            return <li key={idx}>{item.text}</li>;
          };
          return <ul className="results">{this.props.results.map(createItem)}</ul>;
        
      }
    });
Now lets set the results attribute of ResultsList component with some static data:


  //Search Page
    var SearchPage = React.createClass({
    
      render: function() {
        return (
           <div>
             <SearchBar/>
             <ResultsList results={[{text:'some result'}]}  />
           </div>
        );
      }
    });

The result is - we can see the results are displayed:
see code:plnkr

State

Of course the results should not be set that way - statically inside the markup.
They should be defined as member of SearchPage component. For make it accessible for the markup in react we must use state.



  //Search Page
    var SearchPage = React.createClass({
      getInitialState: function() {
        return {results: [{text:'some result'}]};
      },   
      render: function() {
        return (
           <div>
             <SearchBar/>
             <ResultsList results={this.state.results}  />
           </div>
        );
      }
    });

Summary

So now we learned about props and state, and how SearchPage component, communicates with its ResultsList child component. If you find this stuff interesting - continue to the next post

8/2/16

React - Building Search App (Part 2)

Nested Component

So after we learned that react is components and created our first component - lets see how components may host other components inside themselves:
Lets create SearchBar component which be child component of SearchPage and which will include search text input:



    var SearchBar = React.createClass({
      render: function() {
        return (
          <div><input placeholder="type search term"/></div>
        );
      }
    });  
     var SearchPage = React.createClass({
      render: function() {
        return (
          <div>
              <SearchBar/>
          </div>
        );
      }
    });
    
    ReactDOM.render(
        <SearchPage />,
        document.getElementById('example')
      );
And lets add another child component which will hold the list of search results:


     // Results List
    var ResultsList = React.createClass({
      render: function() {
        return (
          <div className="results">here will come search results.. </div>
        );
      }
     });

     ...
     ...
  
     var SearchPage = React.createClass({
      render: function() {
        return (
          <div>
             <SearchBar/>
             <ResultsList/>
          </div>
        );
      }
    });

Note that in react if you want specify some css class you need to use className

Communication

Ok, you asking yourself, but how those components will communicate one with other?
How searchBar will tell the resultsList to display results?
- Here are two words you must remember when you talking about component communication in react:
props and state , we will talk about them in the next part

Getting started with docker

It is very simple to get started usig docker. All you need to do-is download the docker desktop for your system Once you get docker syste...