Answer the question
In order to leave comments, you need to log in
How to display the context of the clicked React component?
There is an example codepen.io/anon/pen/XpzbGM?editors=0011
How can I make it so that when I click on a block with a button, for example, the one where new york is, this data is transferred to the green block, and depending on which block was clicked on the button the city changed, please help :)
Answer the question
In order to leave comments, you need to log in
const locations = [
{
city: "New York"
},
{
city: "Moscow"
}
];
const First = props =>
<div className="first">
<button onClick={props.handleClick}>
click
</button>
<p>{props.city}</p>
</div>;
const Second = props =>
<div className="second">
<span>
{props.city && props.city}
</span>
</div>;
class TestComponent extends React.Component {
constructor() {
super();
this.state = {
selectedCity: false,
};
}
handleClick = city => () => {
this.setState({
selectedCity: city,
});
};
render() {
return (
<div>
{locations.map((data, i) =>
<First
city={data.city}
handleClick={this.handleClick(data.city)}
/>
)}
<Second city={this.state.selectedCity} />
</div>
);
}
}
ReactDOM.render(
<TestComponent />,
document.querySelector('[data-role-id="content"]'));
I want to supplement juicyigor's answer . it contains one critical (for performance) error:
<First
city={data.city}
handleClick={this.handleClick(data.city)}
/>
handleClick = city => () => {
this.setState({
selectedCity: city,
});
};
class First extends React.PureComponent {
handleClick = () => {
const { onClick, city } = this.props
return onClick(city)
}
render() {
const { onClick, city } = this.props
return (
<div className="first">
<button onClick={onClick && this.handleClick}>
click
</button>
<p>{city}</p>
</div>
)
}
}
class TestComponent extends React.PureComponent{
constructor() {
super();
this.state = {
selectedCity: false,
};
}
handleClick = city => {
this.setState({
selectedCity: city,
});
};
render() {
return (
<div>
{locations.map((data, i) =>
<First
city={data.city}
onClick={this.handleClick}
/>
)}
<Second city={this.state.selectedCity} />
</div>
);
}
}
Codepen
For mutable data in the green block, you can use the component's state. You set the state through the function that you pass to props (it's called handleClick everywhere, but you can rename it for convenience). To pass 'city', you can come up with different options, for example, using an data-*
attribute on an element that will be available inside e.target.dataset.*
Why in a constructor - this.XXX = this.XXX.bind(this) - I'm waiting for an answer from you)
Variant with data-city and event delegation.
If the list of locations is permanent, you can create its elements in advance.
const locations = [
{city: "New York"},
{city: "Moscow"}
];
const First = ({city}) => (
<div className="first">
<button data-city={city}>
click
</button>
<p>{city}</p>
</div>
);
const Second = ({city}) => (
<div className="second">
<span>{city}</span>
</div>
);
const list = locations.map((data, i) => <First city={data.city} />);
class TestComponent extends React.Component {
state = {};
handleClick = ({target}) => {
const {city} = target.dataset;
if (city) this.setState({city});
};
render() {
return (
<div onClick={this.handleClick}>
{list}
<Second city={this.state.city} />
</div>
);
}
}
ReactDOM.render(
<TestComponent />,
document.querySelector('[data-role-id="content"]')
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question