mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: Update the template that will be used by `react-native init --template navigation`. The goal is to make it easier for people to get started by demonstrating a few very basic concepts such as navigation, lists and text input. **Test plan (required)** <img src="https://cloud.githubusercontent.com/assets/346214/22697898/ced66f52-ed4a-11e6-9b90-df6daef43199.gif" alt="Android Example" height="800" style="float: left"/> <img src="https://cloud.githubusercontent.com/assets/346214/22697901/cfeab3e4-ed4a-11e6-8552-d76585317ac2.gif" alt="iOS Example" height="800"/> Closes https://github.com/facebook/react-native/pull/12260 Differential Revision: D4521758 Pulled By: mkonicek fbshipit-source-id: d7d9e481dd3373917ac68ec9169b9ac3267547a9
117 lines
2.9 KiB
JavaScript
117 lines
2.9 KiB
JavaScript
|
|
// This file just a dummy example of a HTTP API to talk to the backend.
|
|
// The state of the "database" that would normally live on the server
|
|
// is simply held here in memory.
|
|
|
|
const backendStateForLoggedInPerson = {
|
|
chats: [
|
|
{
|
|
name: 'Claire',
|
|
messages: [
|
|
{
|
|
name: 'Claire',
|
|
text: 'I ❤️ React Native!',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: 'John',
|
|
messages: [
|
|
{
|
|
name: 'John',
|
|
text: 'I ❤️ React Native!',
|
|
},
|
|
],
|
|
}
|
|
],
|
|
};
|
|
|
|
/**
|
|
* Randomly simulate network failures.
|
|
* It is useful to enable this during development to make sure our app works
|
|
* in real-world conditions.
|
|
*/
|
|
function isNetworkFailure() {
|
|
const chanceOfFailure = 0; // 0..1
|
|
return Math.random() < chanceOfFailure;
|
|
}
|
|
|
|
/**
|
|
* Helper for the other functions in this file.
|
|
* Simulates a short delay and then returns a provided value or failure.
|
|
* This is just a dummy example. Normally we'd make a HTTP request,
|
|
* see http://facebook.github.io/react-native/docs/network.html
|
|
*/
|
|
function _makeSimulatedNetworkRequest(getValue) {
|
|
const durationMs = 400;
|
|
return new Promise(function (resolve, reject) {
|
|
setTimeout(function () {
|
|
if (isNetworkFailure()) {
|
|
reject(new Error('Network failure'));
|
|
} else {
|
|
getValue(resolve, reject);
|
|
}
|
|
}, durationMs);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Fetch a list of all chats for the logged in person.
|
|
*/
|
|
async function fetchChatList() {
|
|
return _makeSimulatedNetworkRequest((resolve, reject) => {
|
|
resolve(backendStateForLoggedInPerson.chats.map(chat => chat.name))
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Fetch a single chat.
|
|
*/
|
|
async function fetchChat(name) {
|
|
return _makeSimulatedNetworkRequest((resolve, reject) => {
|
|
resolve(
|
|
backendStateForLoggedInPerson.chats.find(
|
|
chat => chat.name === name
|
|
)
|
|
)
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Send given message to given person.
|
|
*/
|
|
async function sendMessage({name, message}) {
|
|
return _makeSimulatedNetworkRequest((resolve, reject) => {
|
|
const chat = backendStateForLoggedInPerson.chats.find(
|
|
chat => chat.name === name
|
|
);
|
|
if (chat) {
|
|
chat.messages.push({
|
|
name: 'Me',
|
|
text: message,
|
|
});
|
|
resolve();
|
|
} else {
|
|
reject(new Error('Uknown person: ' + name));
|
|
}
|
|
});
|
|
}
|
|
|
|
const Backend = {
|
|
fetchChatList,
|
|
fetchChat,
|
|
sendMessage,
|
|
};
|
|
|
|
export default Backend;
|
|
|
|
// In case you are looking into using Redux for state management,
|
|
// this is how network requests are done in the f8 app which uses Redux:
|
|
// - To load some data, a Component fires a Redux action, such as loadSession()
|
|
// - That action makes the HTTP requests and then dispatches a redux action
|
|
// {type: 'LOADED_SESSIONS', results}
|
|
// - Then all reducers get called and one of them updates a part of the application
|
|
// state by storing the results
|
|
// - Redux re-renders the connected Components
|
|
// See https://github.com/fbsamples/f8app/search?utf8=%E2%9C%93&q=loaded_sessions
|