Skip to content

Instantly share code, notes, and snippets.

@kjendrzyca
Created December 22, 2016 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjendrzyca/f2d0a980481b64b5469d619079bfb98b to your computer and use it in GitHub Desktop.
Save kjendrzyca/f2d0a980481b64b5469d619079bfb98b to your computer and use it in GitHub Desktop.
Post - 13 things you need to know about React
React.createClass({
getInitialState () {
return {
hideTodos: true
}
},
render () {
return (
<div>
<HideIf condition={this.state.hideTodos}>
<TodoList />
</HideIf>
</div>
)
}
})
const HideIf = React.createClass({
render () {
if (this.props.condition) {
return <span>'Sorry there is no data'</span>
}
return this.props.children // children is what's inside <HideIf> element
}
})
<div>
<Greetings color={red} text='Hello' />
</div>
const Greetings = React.createClass({
render () {
const {color, text} = this.props
const divStyle = {padding: 10, backgroundColor: 'black'}
const headingStyle = {color: color}
return (
<div style={divStyle}>
<h1 style={headingStyle}>{text}</h1>
</div>
)
}
})
<select value={this.state.value} onChange={this.handleChange}>
{somearray.map(element => <option value={element.value}>{element.text}</option>)}
</select>
const Parent = React.createClass({
gimmeThatState (textFromInput) {
console.log(textFromInput)
// or this.setState({text: textFromInput})
},
render () {
<InputBox pushChangesUp={this.gimmeThatState} />
}
})
const InputBox = React.createClass({
propTypes: {
pushChangesUp: React.PropTypes.func.isRequired
},
getInitialState () {
return {
text: ''
}
},
changeText (event) {
this.setState({text: event.target.value})
},
pushChangesUp () {
this.props.pushChangesUp(this.state.text)
}
render () {
return (
<div>
<input type='text' onChange={this.changeText} placeholder='text' value={this.state.text} />
<span>{this.state.text}</span>
<button onClick={this.pushChangesUp}>Push changes up</button>
</div>
)
}
})
React.createClass({
getInitialState () {
return {
hideTodos: true
}
},
renderTodos () {
if (this.state.hideTodos) {
return 'Sorry there is no data'
}
return <TodoList />
}
render () {
return (
<div>
{
this.renderTodos()
}
</div>
)
}
})
<select value={this.state.value} onChange={this.handleChange}>
{somearray.map(element => <option value={element.value}>{element.text}</option>)}
</select>
<select onChange={this.handleChange}>
{somearray.map(element => (
<option
value={element.value}
selected={this.state.value === element.value}
>
{element.text}
</option>
))}
</select>
<select
ng-model="selectedItem"
ng-options="item as item.name for item in items">
</select>
const rootElement =
React.createElement('div', {},
React.createElement('h1', {style: {color: 'red'}}, 'The world is yours'),
React.createElement('p', {}, 'Say hello to my little friend')
)
ReactDOM.render(rootElement, document.getElementById('app'))
const RootElement = (
<div>
<h1 style={{color: red}}>The world is yours</h1>
<p>Say hello to my little friend</p>
</div>
)
ReactDOM.render(RootElement, document.getElementById('app'))
const initialState = [
{id: 1, text: 'laundry'},
{id: 2, text: 'shopping'}
// ...
]
const List = React.createClass({
getInitialState () {
return {
todos: initialState
}
},
render () {
return (
<div>
<ul>
{this.state.todos.map(todo => <li key={todo.id}>{todo.text}</li>)}
</ul>
</div>
)
}
})
const initialState = [
{id: 1, text: 'laundry'},
{id: 2, text: 'shopping'}
// ...
]
const List = React.createClass({
getInitialState () {
return {
todos: initialState,
filteredTodos: null
}
},
search (searchText) {
const filteredTodos = this.state.todos(todo => todo.text.indexOf(searchText) > 0)
this.setState({filteredTodos: filteredTodos})
},
render () {
const {filteredTodos, todos} = this.state // get todos from state
const list = filteredTodos === null ? todos : filteredTodos // if there are filtered todos use them
return (
<div>
<SearchBox onChange={this.search} />
<ul>
{list.map(todo => <li key={todo.id}>{todo.text}</li>)}
</ul>
</div>
)
}
})
const initialState = [
{id: 1, text: 'laundry'},
{id: 2, text: 'shopping'}
// ...
]
const List = React.createClass({
getInitialState () {
return {
todos: initialState,
searchText: null
}
},
search (searchText) {
this.setState({searchText: searchText})
},
filter (todos) {
if (!this.state.searchText) {
return todos
}
return todos.filter(todo => todo.text.indexOf(searchText) > 0)
},
render () {
return (
<div>
<SearchBox onChange={this.search} />
<ul>
{this.filter(list).map(todo => <li key={todo.id}>{todo.text}</li>)}
</ul>
</div>
)
}
})
const InputBox = React.createClass({
getInitialState () {
return {
text: ''
}
},
changeText (event) {
this.setState({text: event.target.value})
},
render () {
return (
<div>
<input type='text' onChange={this.changeText} placeholder='text' value={this.state.text} />
<span>{this.state.text}</span>
</div>
)
}
})
// BAD:
// ...
someFunction (value) {
this.setState({someValue: value})
// may not be changed at this point
console.log('New value: ', this.state.someValue)
}
// ...
// GOOD:
// ...
someFunction (value) {
this.setState({someValue: value}, () => {
// do stuff with new state
console.log('New value: ', this.state.someValue)
})
}
React.createClass({
getInitialState () {
return {
hideTodos: true
}
},
render () {
return (
<div>
{
hideTodos ? 'Sorry there is no data' : <TodoList />
}
</div>
)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment