when I declare a use State variable, my react app stops working.. if I uncomment the useState then the code works fine. I don't understand what is the problem?
import React from "react";
import { Helmet } from "react-helmet";
import { Link } from "react-router-dom";
import useState from "react";
import "./login.scss";
const Login = (props) => {
const [credentials, setCredentials] = useState({
user_name: '',
password: '',
})
return (
<div className="application">
</div>
);
}
export default Login;
There could be a few things wrong here:
Are you importing useState from react?
import React, { useState } from "react"
Are you setting value using setText or setting a default state value? Your current example sets default state for text as ''
Are you returning text in the render method? E.g.
return <p>{text}</p>
Are you using React v16.8 or greater? If not hooks will not be available
put curly braces around useState where you imported it like so..
import { useState } from "react";
other than that everything looks fine!
you can potentially delete the line with
import React from "react";
but it's not gonna change anything :)
Instead of:
import useState from "react";
Write:
import { useState } from "react";
Why do I need to add { } to the import?
Answer: When using import on something that was exported with the default keyword - you don't need to wrap the variable name in { }.
However, when using import on something that was exported not as default, you are required to wrap the variable name with { } and say exactly what you want to import.
Related
I'm simply following this tutorial on youtube. useState is giving errors.
Please help me here
import React from 'react'
//import useState from 'react-dom';
import useState from 'react';
export const Tindercards = () => {
const [people, setPeople] = useState([
{
name: "Elon Musk",
url:"https://...." "https://www.biography.com/.image/ar_1:1%2Cc_fill%2Ccs_srgb%2Cfl_progressive%2Cq_auto:good%2Cw_1200/MTc5OTk2ODUyMTMxNzM0ODcy/gettyimages-1229892983-square.jpg"
}
]);
return (
<>
<div className="tindercards">
</div>
</>
)
}
export default Tindercards;
This is the error I got
useState is not the default function from the 'react' package. Change:
import useState from 'react';
to
import { useState } from 'react';
You can leave off the curly braces only for the default function from a module/package.
That error occurred for me when i imported useParams from 'react' while correct import is from 'react-router-dom'.
So, i supposes is directly correlated with fault imports
okay, so i'm new to the react world, and i was learning about the react useContext, i followed exactly what a tutorial on youtube did, i'm trying to build a little project, i tried following his steps and then i hit an error while trying to use the state i literally want it to be accessible accross my app components, inside my app i've created various componenents and also various responsibility that combines all the reusuable components and do a specific task with it. i'm now trying to access my context from my GETCLICKEDIMAGES file, below are my steps
StateContext.jsx file
import React, { useState, createContext } from "react";
export const StateContext = createContext();
export const StateProvider=(props)=> {
//all the components in this app will share this state.
const [names, setNames] = useState([{ name: "Zucci Daniel! its working" }]);
return (
<StateContext.Provider value={"helo"}>{props.children}</StateContext.Provider>
);
}
App.js
import React, { Component } from "react";
import BigWrapper from "./justComponents/BigWrapper/BigWrapper";
import NavBar from "./justComponents/NavBar/NavBar";
import MainContainer from "./justComponents/MainContainer/MainContainer";
//below are the various responsibilities component
import SEARCHANDDISPLAY from "./Responsibilities/SEARCHANDDISPLAY/SEARCHANDDISPLAY";
import { GETCLICKEDIMAGES } from "./Responsibilities/GETCLICKEDIMAGES/GETCLICKEDIMAGES";
import { StateContext } from "./StateContext/StateContext";
class App extends Component {
render() {
return (
<StateContext>
<BigWrapper>
<NavBar />
<MainContainer>
<SEARCHANDDISPLAY />
<GETCLICKEDIMAGES />
</MainContainer>
</BigWrapper>
</StateContext>
);
}
}
export default App;
GETCLICKEDIMAGE.jsx
import React,{useContext} from "react";
import ClickedImageHolderDiv from "../../justComponents/ClickedImageHolderDiv/ClickedImageHolderDiv";
import Figure from "../../justComponents/Figure/Figure";
//you wanna use the stateContext right? import the context here as so;
import { StateContext } from "../../StateContext/StateContext";
//this is responsible for getting the clicked images and displaying them in full details
/**
a DIV to hold the FIGURE
*/
export const GETCLICKEDIMAGES=()=> {
const value =useContext(StateContext);
return (
<ClickedImageHolderDiv>
<h2>{value}</h2>
<Figure useThisStyle={{ height: "100%" }} />
</ClickedImageHolderDiv>
);
}
i don't know if i'm missing something, or something's changed, pls help.
Import StateProvider in your app.js instead of stateContext
When I use the reactn library ( that manage and facilitate hooks) to get my initial global state that I assigned with setGlobal its not setting anything. When I try to get It says undefined.
I have used this library before and it usually is just a matter of importing like :
import React from "reactn";
import { setGlobal } from "reactn";
And then just set the globals.
import React from "reactn";
import { setGlobal } from "reactn";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import * as serviceWorker from "./serviceWorker";
ReactDOM.render(<App />, document.getElementById("root"));
setGlobal({
test: "test",
something: { attr1: "something", attr2: "something", attr3: "something"}
});
serviceWorker.register();
import React, { useEffect, useGlobal } from "reactn";
export default function Engine(props){
const [something, setSomething] = useGlobal("something");
const [test] = useGlobal("test");
console.log(something, test)
useEffect( ()=>{
console.log(something, test)
} );
return(
<div></div>);
}
I expected the values that I'm supposed to get instead of undefined undefined
Issue is that global state is being set (setGlobal) after rendering the App (ReactDOM.render).
Move it above the render and it should work.
Overview
I am having a hard time understanding the syntax I am finding on the net when it comes to wrapping react components with higher order components in order to access props.
The following example (which works) I am to wrapping the component with
withFireStore => ReduxForms => Connect => Component
export default withFirestore(
reduxForm({
form: "createStudents", validate
})(connect(
mapStateToProps,
mapDispatchToProps)(CreateStudentsForm))
);
Background of what I am trying to do
Ultimately I am trying to access props through a Validate function I am using as part of the revalidate.js library. When following the solution from this stackoverflow post, I do not get the props mapped to state from ownProps. I believe this is due to how I an ordering my Higher order components.
Problem
I actually need ReduxForm wrapped by the connect so it can access props I am mapping from redux state.
withFireStore => Connect => ReduxForms => Component
Attempt 1
export default withFirestore(
(connect(
mapStateToProps,
mapDispatchToProps
)
(reduxForm({
form: "createStudents", validate
}))
(CreateStudentsForm)
)
);
Error
Cannot call a class as a function
I thought i had the brackets in the wrong spots, but when I shift them like so I get
Attempt #2
export default withFirestore(
(connect(
mapStateToProps,
mapDispatchToProps
)
(reduxForm({
form: "createStudents", validate
})
(CreateStudentsForm))
)
);
Error
Uncaught Invariant Violation: You must pass a component to the function returned by connect.
Questions
I find chaining functions like this very confusing, is there a better way to write this?
How do I write this so reduxFrom will be wrapped by connect which should give me access to the props within my forms.
Added to question:
CreateStudentForm.js imports
import React, { Component } from "react";
import { reduxForm, Field } from "redux-form";
import {compose} from 'redux';
import { Container, Form, Col, Button } from "react-bootstrap";
import MaterialIcon from '../../material-icon/materialIcon';
import { withFirestore } from "react-redux-firebase";
import { connect } from "react-redux";
import TextInput from "../../forms/textInput";
import { combineValidators, isRequired } from "revalidate";
import { setupStudentForm } from '../../../store/actions/students';
The CreateStudentForm is imported into a Stateful component called modal.js
Modal.js imports
import React, { Component } from "react";
import { connect } from "react-redux";
import { Modal, Button, Row, Col } from "react-bootstrap";
import Aux from '../../hoc/Aux';
import CreateClassForm from "../createClass/createClassForm";
import EditClassForm from "../editClass/editClassForm";
import EditHomeworkForm from "../editHomework/editHomeworkForm";
import CreateHomeworkForm from "../createHomework/createHomeworkForm";
import CreateStudentsForm from "../createStudents/createStudentsForm";
import { withFirestore } from "react-redux-firebase";
try compose, sth like:
export default compose(
withFirestore,
reduxForm({
form: "createStudents", validate
}),
connect(
mapStateToProps,
mapDispatchToProps
)
)(CreateStudentsForm);
I am going through some react code, can any one please let me know what does default as React in below code does.
import {
default as React,
Component,
PropTypes,
} from "react";
Thanks,
Guru
what is default in react import?
Default in that context is the entire React library. It is unnecessary in this case and could be shortened to import React, { Component } from 'react'
Another thing to note, is that proptypes have been moved to their own package now.
That's the same as:
import React, { Component, Proptype } from 'react';
obs: Since react v15.5 React.PropTypes has moved into a different package