Created
December 22, 2016 13:23
-
-
Save kjendrzyca/f2d0a980481b64b5469d619079bfb98b to your computer and use it in GitHub Desktop.
Post - 13 things you need to know about React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<Greetings color={red} text='Hello' /> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
) | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<select value={this.state.value} onChange={this.handleChange}> | |
{somearray.map(element => <option value={element.value}>{element.text}</option>)} | |
</select> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Parent = React.createClass({ | |
gimmeThatState (textFromInput) { | |
console.log(textFromInput) | |
// or this.setState({text: textFromInput}) | |
}, | |
render () { | |
<InputBox pushChangesUp={this.gimmeThatState} /> | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
) | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
) | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<select value={this.state.value} onChange={this.handleChange}> | |
{somearray.map(element => <option value={element.value}>{element.text}</option>)} | |
</select> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<select onChange={this.handleChange}> | |
{somearray.map(element => ( | |
<option | |
value={element.value} | |
selected={this.state.value === element.value} | |
> | |
{element.text} | |
</option> | |
))} | |
</select> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<select | |
ng-model="selectedItem" | |
ng-options="item as item.name for item in items"> | |
</select> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
) | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
) | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
) | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
) | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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