1

I have some data called channels which is an array containing objects:

channels : [
  { name:'tv100',number:'100'},
  { name:'tv200',number:'200'}
]

My components in react:

ChannelContainer:

export default class ChannelContainer extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            channels: '',
            selectedChannel: ''
        }
    }

    componentDidMount() {
        let channel_data = [{ name: 'tv100', number: '100' }, { name: 'tv200', number: '200' }];
        this.setState = { channels: channel_data }
    }

    render(){
        return (
            <div className="tv">
                <TvGuide channel={this.state.channels} />
                <TvView channel_nr={this.state.selectedChannel} />
            </div>
        );
    }
}

TvGuide component:

const TvGuide = (props) => {
    return (
        <div className="tvguide_container">
            <ul>
                {props.channel.map((number, index) => {
                    return(
                        <li key={index}>
                            <a> {number}</a>
                        </li>
                    )   
                })
                }
            </ul>
        </div>
    );  
}

TvView component:

const TvView = (props) => {
    return (
        <div>
            <h1>{props.channel.name}</h1>
        </div>
    )
}
export default TvView

When a user click on one of the links in TvGuide component I would like to send props data to TvView component so it can render the name of the channel. What is the best way to go forward with this in react?

1 Answer 1

1

From ChannelContainer I would pass in a onClick handler to TvGuide which would be called with the channel clicked on, and this would call setState in the container, thereby rerendering your TvView.

class ChannelContainer extends React.Component {

    // ...

    onGuideClick = (channel) => {
        this.setState({
            selectedChannel: channel
        })
    }

    render(){
        return (
            <div className="tv">
                <TvGuide onClick={this.onGuideClick} channel={this.state.channels} />
                <TvView channel_nr={this.state.selectedChannel} />
            </div>
        );
    }
}


const TvGuide = (props) => {

    return (
        <div className="tvguide_container">
            <ul>
                {props.channel.map((channel) => {
                    const onClick = () => {
                        props.onCLick(channel);
                    }
                    return(
                        <li onClick={onClick} key={channel.number}>
                            <a>{channel.name}</a>
                        </li>
                    )
                })
                }
            </ul>
        </div>
    );
}
Sign up to request clarification or add additional context in comments.

4 Comments

can you provide an code example? Just wanna get my head around it.
sure, I added some, untested code which follows the description
A better abstraction is to have a onChannelSelected event, that way the event can be fired if you offer keyboard navigation, or if it's programatically changed. Also, why are you creating a separate handler for each channel? Can't you just do onClick="this.onClick(channel)"
@enjoylife nice, can I set a default value so it start with index 0 in the array object. Something like channel = channel || this.state.selectedChannel ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.