React dealing with radios button

By default when you create radio multiple radios with same names, check either one will uncheck the rest.

  
  
  

Try out above codes.

In jQuery we can get the value of the radio easily using:

$('input[name="radio1"]').val();

In react we have use state to get the value of current selected radio:

  class Test extends React.Component {
      handleRadioSelection(e){
      console.log(e.target.value);
  }
  
	render(){
		return (
      	
1
2
3
4
) } } React.render(, document.getElementById('container'));

Continue reading “React dealing with radios button”

React dealing with controlled component input forms

In most case react form will works normally until you wanted to set a value in the input and change while changes / start to input in something,

<input type="text" value="some value here" />

and you realised the value does not change no matter how hard you pressed, this is because of the controlled component:

“In HTML, form elements such as <input>, <textarea>, and <select> typically maintain their own state and update it based on user input. In React, mutable state is typically kept in the state property of components, and only updated with setState().”

Continue reading “React dealing with controlled component input forms”

react passing props to this.props.children

Previous article I mentioned about this.props.children and its feature, problem is this.props.children does not not provide a way to pass down the props, so we need a wrapper called React.cloneElement which allows you get this component and return a new component where you can also pass in the props like this:

{React.cloneElement(this.props.children, this.props)}

by the way there are more very useful helpers to help you interacting with this.props.children, for example:

React.children.map(this.props.children,()=>{…})

React.Children.forEach(this.props.children, (child, i) => {
  console.log(child + ' at index: ' + i);
})

see all API at React.children

React double scrollbar component

While playing with react, I have built a react component which server as second scrollbar for your overflow divs.
You are free to use the code that I added to github…

https://github.com/maxlibin/react-doublescrollbar

React pass parameter to parent component or call parent function on ES6

sometime you will need to call parents function on react component, the problem in ES6 is that all functions are not bind with this, so you will need to do manually, below is a simple toggle function that you will call from Child component, notice that in order to call the parent’s method, you will have to pass in the function as a property to Child component then trigger from child onClick method, notice that I have bind this to this.props.parentToggle, by default in ES6 react decided not to auto bind this like what usual javascript does, so we have to do it manually.

Continue reading “React pass parameter to parent component or call parent function on ES6”