To start i am sorry i am a new on native react
I have a project with react navigation who show this component.
import React, { Component } from 'react';
import {FlatList,StyleSheet,View,TouchableHighlight,Text} from 'react-native'
import {
Container,
Button,
ListItem,
Left,
Right,
Icon,
Body
} from 'native-base';
import Customer from '../Customer';
import Search from '../../../components/Search'
export default class SearchCustomer extends Component {
constructor(props) {
super(props);
this.state = {
customerList:[]
}
}
render() {
return (
<Customer>
<Search
setCustomerList = {(customerList) => {this.setState({customerList})}}
/>
<FlatList
data={this.state.customerList}
keyExtractor={item => item.id}
renderItem={({ item, index}) => (
<ListItem onPress={(item) => this.props.callback()}>
<Left style={styles.left}>
<Text>{item.firstname} {item.lastname}</Text>
<Text style={styles.subtitle}>{item.email}</Text>
</Left>
<Right>
<Icon name='arrow-forward' />
</Right>
</ListItem>
)}/>
</Customer>
)
}
}
This component call his parent that here below
import React, { Component } from 'react';
import {
Container,
Button,
Text,
} from 'native-base';
import Order from '../Order';
export default class Customer extends Component {
constructor(props) {
super(props);
this.state = {
}
}
render() {
return (
<Order>
{this.props.children}
</Order>
)
}
}
I want to know how can i send data from the child to his parent with this configuration.
Currently i am trying to catch this.props.callback() in the parent but i can't use this callback={() => {console.log('Ok')}}
I have this error
Someone have a solution ?
Using some of your class you can define a method in your parent class then pass the function as props to child
export default class Customer extends Component {
constructor(props) {
super(props);
this.state = {
}
}
callback = (data) => { console.log(data) }
render() {
return (
<Order callback={this.callback}>
{this.props.children}
</Order>
)
}
}
Then from child you can provide the data in the callback for parent.
export default class Order extends Component {
constructor(props) {
super(props);
this.state = {
}
}
render() {
return (
<TouchableOpacity onPress={() => this.props.callback('hi')}>
<Text>Click Me!</Text>
</TouchableOpacity >
)
}
}
read this for more good understanding : https://reactjs.org/tutorial/tutorial.html#passing-data-through-props
Related
I'm relatively new to react & can't seem to figure it out, I have researched this for sometime now & nothing seems to work.
I have a parent component where I'm using createRef
class Parent extends React.Component {
constructor(props) {
super(props);
this.chartRef = React.createRef();
}
Then pass it to child & access is like following
<Grid item xs={12}>
<Child
ref={this.chartRef}
/>
<Button onClick={this.getState}> get ref info</Button>
</Grid>
But in getState chartRef current is always null
getState = () => {
console.log(this.chartRef.current);
};
Here is the child component
class Child extends React.Component {
componentDidMount() {
this.props.registerPlugins.forEach(plugin => {
Chart.pluginService.register(plugin);
});
}
render = () => {
const { data, options, plugins, height } = this.props;
const updatedOptions = {
...options
};
return <div>
<Line
height={height}
data={data}
options={updatedOptions}
plugins={plugins}/>
</div>;
};
}
Child.propTypes = {
height: PropTypes.number.isRequired,
data: PropTypes.object.isRequired,
options: PropTypes.object.isRequired,
plugins: PropTypes.array,
registerPlugins: PropTypes.array,
};
export default Child;
Any help is appreciated
You could use the callback style ref E.g.instead of passing your ref as the prop, you can pass in a reference to a function like this.handleRef
handleRef = r => {
this.chartRef.current = r;
};
<Child ref={this.handleRef} />
You can access child component in parent with help of reference as like below
import React from "react";
import { Button, View } from "react-native";
import Child from "./Child";
class App extends React.Component {
constructor() {
super();
this.chartRef = React.createRef();
}
render() {
return (
<View>
<Child ref={(r) => (this.chartRef = r)} />
<Button
title="Parent Button"
onPress={() => {
console.log(this.chartRef);
}}
></Button>
</View>
);
}
}
export default App;
// Child.js
import React from "react";
import { Button } from "react-native";
export default class Child extends React.Component {
constructor() {
super();
}
render() {
return (
<Button
title="Child Button"
onPress={() => {
alert("Child");
}}
></Button>
);
}
}
you can try this code on codesandbox example
I take photo to demonstrate, you may look at it:
My parent component is 1, grand child component place at 3, at 2 I can do some stuff because they is parent-child but I can't call function of 1 when I look from 3, any your interest, thanks!
you have to pass props from child to grandchild
here is an example (i use react native)
Parent.js
import React, { Component } from 'react';
import Child from './Child';
import { Button, Text, View } from 'react-native';
export default class Parent extends Component {
constructor(props) {
super(props);
this.state = {
result: 'original state'
}
}
stateUpdate(data) {
this.setState({result:data})
}
render() {
return (
<View>
<Text>parent state: {this.state.result}</Text>
<Button
onPress={() => this.stateUpdate('original state')}
title="original state"
color="darkgreen"
/>
<Child data={this.state.result} updateState={(data) => this.stateUpdate(data)} />
</View>
)
}
}
Child.js
import React, { Component } from 'react';
import GrandChild from './GrandChild';
import { Button, Text, View } from 'react-native';
export default class Child extends Component {
constructor(props) {
super(props);
}
render() {
return (
<View>
<Text style={{height:20}} />
<Text>child prop: {this.props.data}</Text>
<Button
onPress={() => this.props.updateState('Changed from Child')}
title="change parent state"
color="#841584"
/>
<GrandChild data={this.props.data} updateParent={(data) => this.props.updateState(data)} />
</View>
)
}
}
GrandChild.js
import React, { Component } from 'react';
import { Button, Text, View } from 'react-native';
export default class GrandChild extends Component {
constructor(props) {
super(props);
}
render() {
return (
<View>
<Text style={{height:20}} />
<Text>GrandChild prop: {this.props.data}</Text>
<Button
onPress={() => this.props.updateParent('change from GrandChild')}
title="change parent state"
color="darkblue"
/>
</View>
)
}
}
here is a snack
https://snack.expo.io/GsuAf65kK
or use context api.
hope it help.
I am trying to send value of TextInput to another Class Function in console.log. My approach is when the button is pressed the value FromStr in TextInput will got passed into another class function. Here's my code
import React, { Component } from "react";
import { StyleSheet, Text, View, TextInput, Button } from "react-native";
import receiveMessage from "./receiveMessage"
export default class WeatherProject extends Component{
constructor (props) {
super(props);
this.state={
From:'',
FromStr:'',
}
}
changeText=(From)=>{
this.setState({From})
}
changeText1=(To)=>{
this.setState({To})
}
onPress = ()=>{
this.setState({FromStr: this.state.From})
receiveMessage.receiveMessage();
}
render(){
return (
<View>
<View style={styles.inputFields}>
<TextInput placeholder="From" id="from" style={styles.fromField} onChangeText={this.changeText} />
<View style={styles.buttonStyle}>
<Button
title={"Go Back"}
color="#f194ff"
onPress={this.onPress}
></Button>
</View>
</View>
</View>
);
}
}
receiveMessage.js
import React, { Component } from "react";
export default class receiveMessage extends Component {
static receiveMessage=()=>{
console.log(this.state.FromStr)
}
}
React does not allow to pass the data between react components in this way.
Following is way to pass the data between components in React. To get more insights please follow
import React, { Component } from 'react';
class WeatherProject extends Component {
render() {
const messageToPassed = 'Hello';
return (
<div>
<ReceiveMessage message={messageToPassed} />
</div>
);
}
}
const ReceiveMessage = props => <h1>{props.message}</h1>;
export default App;
here we pass the value from sidemenu component by raising an event
App.js
class App extends React.Component {
handleSubmit = (e, data) => console.log(`my data from props`, data);
render() {
return (
<Sidemenu
onSubmit={(e, data)=>this.handleSubmit(e, data)} />
);
}
}
SideMenu.js
const Sidemenu = props => {
const { onSubmit} = props;
return (
<button onClick={(e, type)=>this.onSubmit(e, 'mydata')} />
);
}
Can I use children in React Container or is it wrong?
For example, I have a list of buttons(ActionButton) that are grouped together (ActionMenu).
import React from 'react';
class App extends React.Component {
render() {
return (
<ActionMenu>
<ActionButton name="New" icon="add" />
<ActionButton name="Delete" icon="remove" />
</ActionMenu>
)
}
}
class ActionMenu extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
alert('Click!');
}
render() {
return React.Children.map(this.props.children, (button) =>
React.cloneElement(button, {
onClick: this.handleClick
})
);
}
}
function ActionButton({ name, icon, onClick }) {
return <button class={icon} onClick={onClick}>{name}</button>
}
You can use children regardless of whether it's a component of container.
"[children are] especially common for components like Sidebar or Dialog that represent generic 'boxes'."
In your case you have a menu, which falls into this category.
https://reactjs.org/docs/composition-vs-inheritance.html
I think this is what you are after. Actually you should just put the children in its closest parent instead of its grandpa.
import React from 'react';
import { render } from 'react-dom';
function ActionButton({ name, handleClick }) {
return <button onClick={handleClick}>{name}</button>
}
class ActionMenu extends React.Component {
constructor(props) {
super(props);
}
handleClick = () => {
alert('Click!');
}
render() {
return (
<div>
<ActionButton name="add" handleClick={this.handleClick}/>
<ActionButton name="remove" handleClick={this.handleClick} />
</div>
);
}
}
class App extends React.Component {
render() {
return (
<ActionMenu />
)
}
}
render(<App />, document.getElementById('root'));
You can try to run it in sandbox.
By the way, using bind is quite redundant now, we can use public class fields syntax, which is already ECMA stage 2.
When i try to use DatePickerIOS with react native nothing happen in my phone ...
my code :
import React, { Component } from 'react';
import {Text, View, DatePickerIOS} from 'react-native';
export default class DatePicker extends Component {
constructor(props) {
super(props);
this.state = {
today: new Date(),
}
}
render() {
return (
<View>
<DatePickerIOS date={this.state.today} mode="time" onDateChange={(value) => this.setState({today: value})}/>
</View>
);
}
}
SomeOn know why ?
If you want to print selected time in the UI add the following to the render
render() {
return (
<View>
<DatePickerIOS
date={this.state.today}
mode="time"
onDateChange={(value) => this.setState({today: value})}
/>
<Text>{this.state.today.toTimeString()}</Text>
</View>
);
}