how navigate screen of two different stacks in react native/expo - expo

create product stack navigation having two screen product and check out
const ProductStack = createStackNavigator();
function ProductStackNavigation() {
return (
<ProductStack.Navigator initialRouteName="Product">
<ProductStack.Screen
name="Product"
options={{
headerTitle: "Product",
headerShown: true,
}}
component={Product} />
<ProductStack.Screen
name="CheckOut"
options={{
headerTitle: "CheckOut",
headerShown: true,
}}
component={CheckOut} />
</ProductStack.Navigator>
)
}
---
**create other favourite stack navigation having one screen favourite**
const FavouriteStack = createStackNavigator();
function FavouriteStackNavigation() {
return (
<FavouriteStack.Navigator initialRouteName="Favourite">
<FavouriteStack.Screen
name="Favourite"
options={{
headerTitle: "Favourite",
headerShown: true,
}}
component={Favourite} />
</FavouriteStack.Navigator>
)
}
create tab navigation conatain two stack first contain product stack and second contain favourite stack navigate from favourite screen to product screen
const BottomTab = createBottomTabNavigator();
function TabNavigation() {
return (
<BottomTab.Navigator
tabBarOptions={{
activeTintColor: '#fafafa',
labelStyle: { marginBottom: 10 }
}}
>
<BottomTab.Screen
name="product"
children={() => <ProductStackNavigation />}
options={{
tabBarLabel: 'product',
}}
/>
<BottomTab.Screen
name="Favourite"
children={() => <FavouriteStackNavigation />}
options={{
tabBarLabel: 'Favourite',
}}
/>
</BottomTab.Navigator>
)
}
how to navigate favourite screen to checkout screen inside favourite tab

You actually dont need to create "FavouriteStack". You can simply place the component Favourite in the BottomStack.Screen like this:
<BottomTab.Screen
name="Favourite"
component={Favourite}
options={{
tabBarLabel: 'Favourite',
}}
/>
And regarding navigation from Favourite to Checkout you can use useNavigation hook from "#react-natvigation/native"
const navigation = useNavigation();
and perform navigation using
navigation.navigate("ProductStack", {
screen:"Checkout"
})
Remember you need to have ProductStack as screen as well somewhere so that you can access that stack. You cant navigate to detached stacks from main navigation container. You can only switch the stacks using ternary operator.
isFavourite ? <FavouriteNavigator /> : <Productnavigator />

Related

Svelte-ChartJS chart binding

I am trying to use svelte-chartjs with chartjs-plugin-zoom. In order to programmatically adjust zoom. In order to do this you have to bind onto the element. This can be illustrated in the React-ChartJS-2 - see buttons below.
Is there a prop in svelte-chartjs to make that binding?
Something like:
<Line chartRef={myrefvar} {options} />
Use bind:property (tutorial) to get the chart reference ยป REPL
<script>
...
let chartRef
const onZoomPluse = () => {
chartRef.zoom(1.1);
};
</script>
<Line bind:chart={chartRef} options={options} data={data} />
<button on:click={onZoomPluse}>zoom +10%</button>

How to autofocus an input field in semantic-ui-react?

I'm having a difficult time autofocusing an input field with semantic-ui-react. The documentation doesn't seem to include an autoFocus prop and the focus prop doesn't place the cursor inside the input field as would be expected.
<Form onSubmit={this.handleFormSubmit}>
<Form.Field>
<Form.Input
onChange={e => this.setState({ username: e.target.value })}
placeholder='Enter your username'
fluid />
</Form.Field>
</Form>
EDIT: This code works:
<Form onSubmit={this.handleFormSubmit}>
<Form.Input
onChange={e => this.setState({ username: e.target.value })}
placeholder="Enter your username"
autoFocus
fluid />
</Form>
The focus prop is purely to add a focus effect on the input's appareance, it does not actually set the focus.
Any props unused by Semantic are passed down to the DOM element, so if you set an autoFocus prop, it should go down to the input.
However, as explained in the Form documentation:
Form.Input
Sugar for <Form.Field control={Input} />.
So your code should rather be:
const yourForm = (
<Form onSubmit={this.handleFormSubmit}>
<Form.Input
onChange={e => this.setState({ username: e.target.value })}
onSelect={() => this.setState({ usernameErr: false })}
placeholder="Enter your username"
error={usernameErr}
iconPosition="left"
name="username"
size="large"
icon="user"
fluid
autoFocus
/>
</Form>
)
Note that this only works if you want the focus to happen right when the wrapper component is mounted. If you want to focus the input after it has been mounted, you have to use a ref and call the focus() method on it, just as showed in the documentation, like so:
class InputExampleRefFocus extends Component {
handleRef = (c) => {
this.inputRef = c
}
focus = () => {
this.inputRef.focus()
}
render() {
return (
<div>
<Button content='focus' onClick={this.focus} />
<Input ref={this.handleRef} placeholder='Search...' />
</div>
)
}
}
Hope that helps!
I would have assumed that semantic UI would pass all unknown props to the root element, the input. So if it does, you should be able to add the autoFocus attribute to it, if not, you will have to control which input is being focused in your state.
<Input placeholder='Search...' focus={this.state.focusedElement === "search"}/>
In order to tell the input field to focus, you need to create a reference (ref) to the input field as follows:
import React, { useState, useRef } from 'react';
import { Input, Button } from 'semantic-ui-react';
const SearchInputExample = () => {
const [searchValue, setSearchValue] = useState('');
// Create reference to the input field
const searchRef = useRef(null);
const handleSearchValueChange = event => setSearchValue(event.target.value);
return (
<div>
<Input
placeholder="Search..."
// Assign the ref created to a ref attribute
ref={searchRef}
value={searchValue}
onChange={handleSearchValueChange}
/>
<Button
onClick={() => {
setSearchValue('');
// Use the ref assigned to put the focus inside the input
searchRef.current.focus();
}}
>
Clear search (and focus)
</Button>
</div>
);
};
export default SearchInputExample;
You can read more about the useRef() hook here

Checkbox label with URL link in Semantic UI React

Semantic UI React Checkbox can be used like this:
import React from 'react'
import { Checkbox } from 'semantic-ui-react'
const TermsOfServiceCheckbox = () => (
<Checkbox label='I accept the Terms of Service' />
)
export default TermsOfServiceCheckbox
How do I set the Checkbox label so that the text Terms of Service is a link to a URL?
label prop implements the item shorthand, so you can also pass there:
<Checkbox label={<label><a href='/'>I accept the Terms of Service</a></label>} />
<Checkbox label={<label>I accept the <a href='/'>Terms of Service</a></label>} />
<Checkbox label={{ children: <a href='/'>I accept the Terms of Service</a> }} />

Is duplicating state of props the only way to go in React.js?

Take a look at the source code given below. It models a form that is composed out of two WHOIS components and a radio button group. The radio button determines the WHOIS component that is selected. Once the selected WHOIS component is valid the form should be submittable and emit the data of the selected WHOIS.
/** #jsx React.DOM */
var Publish = React.createClass({
getInitialState: function() {
return {
type: 'intern',
intern: {
domain: '',
valid: false
},
extern: {
domain: '',
valid: false
}
};
},
updateWhois: function(type, domain, state) {
var newState = {};
newState[type] = {
domain: domain,
valid: state === 'free'
};
this.setState(newState);
},
switchTo: function(type) {
this.setState({
type: type
});
},
isValid: function() {
var type = this.state.type;
var typeState = this.state[type];
return typeState.valid;
},
render: function() {
return (
<form className="publish">
<div>
<input name="publish-type" type="radio" checked={this.state.type === 'intern'} onChange={this.switchTo.bind(this, 'intern')} />
<Whois onFocus={this.switchTo.bind(this, 'intern')} onChange={this.updateWhois.bind(this, 'intern')} />
</div>
<div>
<input name="publish-type" type="radio" checked={this.state.type === 'extern'} onChange={this.switchTo.bind(this, 'extern')} />
<Whois onFocus={this.switchTo.bind(this, 'extern')} onChange={this.updateWhois.bind(this, 'extern')} />
</div>
<input type="submit" disabled={!this.isValid()} />
</form>
);
}
});
This code works perfectly. However I feel I am duplicating the state of the child components. How could I improve this example? Upon submit I would rather query the Whois component directly (for example an .getDomain method), but I have not clue if and how I should do so in ReactJs.
I feel as though there's nothing directly wrong with your example here, as there are different paradigms you can use when writing ReactJS components. If you do want to read from a rendered component, look at the methods in more about refs. Adding a ref property to your whois like so:
<Whois onFocus={this.switchTo.bind(this, 'intern')} onChange={this.updateWhois.bind(this, 'intern')} ref="whoIs1" />
this.refs.whoIs1.getDOMNode();
would make it possible to look at the component after it has been rendered into the DOM. Using the event argument inside a handler for your onChange method would also be a good option, as you can query the component's state out of the properties on event.

Windows 8 JS & html5 app - foreach within template

How do I loop thru items in a WinJS.Binding.Template within my WinJS.UI.ListView control?
My data:
{ category: 'Sports',
items: [
{ title: 'soccer'}, { title: 'tennis'}
]
}
What I want to do in my template:
<div id="myTmpl" data-win-control="WinJS.Binding.Template" style="display:none">
<h1 data-win-bind="innerText: category"></h1>
<div data-win-REPEATER="each: items">
<span data-win-bind="innerText: title"></span>
</div>
</div>
Because you want to nest the details of the items, I would recommend that you look at my answer to another question, which is very similar:
WinJS ListView and Template Binding
This has all the essential details.
That stated, theres no reason you cant integrate JQuerys templates here - you just need to find a way for them to work with the WinJS template / control contract.
Have you checked
http://code.msdn.microsoft.com/windowsapps/ListView-basic-usage-sample-fcc451db
Normally the loop is automatically (it will do based on the datasource information)
<div id="listView"
class="win-selectionstylefilled"
data-win-control="WinJS.UI.ListView"
data-win-options="{
itemDataSource: myData.dataSource,
itemTemplate: myTmpl,
selectionMode: 'none',
tapBehavior: 'none',
swipeBehavior: 'none',
layout: { type: WinJS.UI.GridLayout }
}"
></div>