dimanche 5 novembre 2017

How to pass a value to other component in React

I have a project that can add, edit or delete movies from a list. I'm having trouble with the edit part of the component, next to every movie I added a text input to edit that movie if wanted, but I don't know how to pass that value to the other component in order to change the old value for the new one. Here is what I got for now: Movie component:

import React, {Component} from 'react';


class Movie extends Component{
    constructor(props){
        super(props);
        this.state= {
            inputValue: ''
        };
    }
    updateInputValue(evt) {
        this.setState({
            inputValue: evt.target.value
        });
    }

    render(){
        return(
            <div>
                {this.props.text}
                <button onClick={this.props.deleteMethod}>X</button>
                <input value={this.props.newMovieName} 
                    onChange={evt => this.updateInputValue(evt)}
                /> 
                <button onClick={this.props.editMethod().bind(this.inputValue)}>edit</button>


            </div>
        );
    }
}

export default Movie;

Add component:

import React,{Component} from 'react';
import Movie from './Movie.jsx';


class AddComponent extends Component {
    constructor(props){
        super(props);
        this.state = {
            movieText: '',
            movies: [],
        };
    }

updateMovieText(movieText){
    this.setState({movieText: movieText.target.value})
}

addMovie(){
    if(this.state.movieText === ''){return}

    let moviesArr = this.state.movies;
    moviesArr.push(this.state.movieText);
    this.setState({movieText: ''})
    this.textInput.focus();
}

handleKeyPress = (event) => {//enables to add when pressing enter on keyboard
    if(event.key === 'Enter'){
    let moviesArr = this.state.movies;
    moviesArr.push(this.state.movieText);
    this.setState({movieText: ''})
    }
}

deleteMovie(index) {
    let movieArr = this.state.movies;
    movieArr.splice(index,1);//remove the movie from array
    this.setState({movies: movieArr})
}

editMovie(index,value){
    let moviesArr = this.state.movies;
    moviesArr[index] = value;
    this.setState({movies:movieArr});
}

    render(){
        let movie = this.state.movies.map((val,key)=> {//prints on screen list of movies see line55
            return (<Movie 
            key={key} 
            text={val} 
            deleteMethod={() => this.deleteMovie(key)}
            editMethod={() => this.editMovie(key,this.inputValue)}
             />

            );

        });

        return (
            <div>
                <input type="text"
                    ref={((input)=>{this.textInput = input;})}
                    className="textInput"
                    value={this.state.movieText}
                    onChange={movieText => this.updateMovieText(movieText)}
                    onKeyPress={this.handleKeyPress.bind(this)}
                    />
                <button onClick={this.addMovie.bind(this)}>Add</button>
                {movie}

                </div>

        );
    }
}

export default AddComponent;

I think I should use bind, but for now it isn't working




Aucun commentaire:

Enregistrer un commentaire