Answer the question
In order to leave comments, you need to log in
How to properly pass data from component to component without redux?
Good afternoon, I asked this question, but I will rephrase it with code.
There is a component that stores input :
class someClass extends React.Component {
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
items: []
};
}
state = {
searchValue: ''
}
onSearchChange = (event, searchValue) => {
this.setState({ searchValue })
}
onSearchClick = () => {
const { searchValue } = this.state;
fetch("someuri?name="+searchValue)
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
items: result.items
}
);
},
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
.catch(error => console.log('parsing failed', error));
}
return(
<Input leftIcon={<Icon name="search" />}
value={this.state.searchValue}
width="750px"
placeholder="Введите Фамилию и Имя"
onChange={this.onSearchChange}
/> {' '}
<Button use="primary" onClick={this.onSearchClick}>Найти</Button>
)
class SearchHub extends Component{
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
items: []
};
}
render(){
const { error, isLoaded, items } = this.state;
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <Loader type="big" active> </Loader>;
} else {
return (
<div className={styles.parent}>
<div>
{items.map(item => (
<Gapped gap={20} key={item.name}>
<span><b>{item.rating.totalLove}</b></span>
<span><img src={shape} alt="heart"/></span>
<span><img src={item.avatarUri} alt="avatar" className={styles.photo}/></span>
<span>{item.name}<br/>{item.role}</span>
</Gapped>
))}
</div>
</div>
);
}
}
}
Answer the question
In order to leave comments, you need to log in
But how can I pass them to my daughter for display?
<SearchHub error={error} isLoaded={isLoaded} items={items} />
class SearchHub extends Component {
render(){
const { error, isLoaded, items } = this.props;
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <Loader type="big" active> </Loader>;
} else {
return (
<div className={styles.parent}>
<div>
{items.map(item => (
<Gapped gap={20} key={item.name}>
<span><b>{item.rating.totalLove}</b></span>
<span><img src={shape} alt="heart"/></span>
<span><img src={item.avatarUri} alt="avatar" className={styles.photo}/></span>
<span>{item.name}<br/>{item.role}</span>
</Gapped>
))}
</div>
</div>
);
}
}
}
strange question, if it is asked correctly, then the question is how to pass data to child components, the answer is obvious - props
if the task is to transfer from one component to another (when none of the components is a parent or child), then they can be wrapped in one component, the sending component must have a callback function from the stateOnChanged series, in the parent this change is fixed in the state to which the receiving component is subscribed, then the data also runs through the props.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question