'Creating a button in React
I am learning react and was told Remember to add a button on the login-view or the main-view to get to the registration view && Create a button or a link for unregistered users somewhere in your main view or login view. You don’t have to implement a function on the button/link at this time.
I am not sure one if its better to add it to my main-view.jsx or login-view.jsx, but I am not sure what I should be searching for in regards to a solution as I am a bit lost.
Here is my main-view.jsx & login-view.jsx code:
import React from 'react';
import axios from 'axios';
import { MovieCard } from '../movie-card/movie-card';
import { MovieView } from '../movie-view/movie-view';
import { LoginView } from '../login-view/login-view';
import { RegistrationView } from '../registration-view/registration-view';
export class MainView extends React.Component {
constructor() {
super();
this.state = {
movies: [],
selectedMovie: null
}
}
componentDidMount(){
axios.get('https://mymovies-4523.herokuapp.com/movies')
.then(response => {
this.setState({
movies: response.data
});
})
.catch(error => {
console.log(error);
});
}
// constructor() {
// super();
// this.state = {
// movies: [
// { _id: 1, Title: 'Inception', Description: 'A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into the mind of a C.E.O., but his tragic past may doom the project and his team to disaster.', Genre: 'Sci-Fi', Director: 'Christopher Nolan',ImagePath: 'https://m.media-amazon.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_.jpg' },
// { _id: 2, Title: 'The Shawshank Redemption', Description: 'Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.', Genre: 'Drama', Director: 'Frank Darabont', ImagePath: 'https://m.media-amazon.com/images/M/MV5BMDFkYTc0MGEtZmNhMC00ZDIzLWFmNTEtODM1ZmRlYWMwMWFmXkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_FMjpg_UX1000_.jpg' },
// { _id: 3, Title: 'Gladiator', Description: 'A former Roman General sets out to exact vengeance against the corrupt emperor who murdered his family and sent him into slavery.', Genre: 'Adventure', Director: 'Ridley Scott', ImagePath: 'https://m.media-amazon.com/images/M/MV5BMDliMmNhNDEtODUyOS00MjNlLTgxODEtN2U3NzIxMGVkZTA1L2ltYWdlXkEyXkFqcGdeQXVyNjU0OTQ0OTY@._V1_.jpg' }
// ],
// selectedMovie: null
// };
// }
setSelectedMovie(newSelectedMovie) {
this.setState({
selectedMovie: newSelectedMovie
});
}
onLoggedIn(user) {
this.setState({
user
});
}
render() {
const { movies, selectedMovie, user, register } = this.state;
if (!register) return (<RegistrationView onRegistration={(register) => this.onRegistration(register)} />);
if (!user) return <LoginView onLoggedIn={user => this.onLoggedIn(user)} />;
if (movies.length === 0) return <div className="main-view" />;
return (
<div className="main-view">
{selectedMovie
? <MovieView movie={selectedMovie} onBackClick={newSelectedMovie => { this.setSelectedMovie(newSelectedMovie); }}/>
: movies.map(movie => (
<MovieCard key={movie._id} movie={movie} onMovieClick={(newSelectedMovie) => { this.setSelectedMovie(newSelectedMovie) }}/>
))
}
</div>
);
}
}
export default MainView;
I added a button in this file, but I don't see anything. So I do not think this is correct login-view.jsx:
import React, { useState } from 'react';
export function LoginView(props) {
const [ username, setUsername ] = useState('');
const [ password, setPassword ] = useState('');
const handleSubmit = (e) => {
e.preventDefault();
console.log(username, password);
/* Send a request to the server for authentication */
/* then call props.onLoggedIn(username) */
props.onLoggedIn(username);
};
function Button({label}) {
return (
<button>{label}</button>);
}
return (
<form>
<label>
Username:
<input type="text" value={username} onChange={e => setUsername(e.target.value)} />
</label>
<label>
Password:
<input type="password" value={password} onChange={e => setPassword(e.target.value)} />
</label>
<button type="submit" onClick={handleSubmit}>Submit</button>
</form>
);
}
If anyone could point me in the right direction, it would be much appreciated. Thank you for your time!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|