Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
92
rated 0 times [  96] [ 4]  / answers: 1 / hits: 15570  / 6 Years ago, mon, december 3, 2018, 12:00:00

Im having trouble with the following code:



My compnent:



class Landing extends Component {

state = {
loginMethod: '',
tournamentCode: -1,
}
}


My Const:



const Code = () => (
<div className= style={{marginTop: 20}}>
<input
className=
style={{lineHeight: 1, height: 30, border:0, paddingLeft: 5}}
placeholder=Tournament Code
onChange={e => this.setState({tournamentCode: e.target.value})} />
<Link to=/App>
<button className= style={{marginLeft: 10, border: 0, outline:
'none', background: '#efefef', height: 30, width: 80}}> Enter </button>
</Link>
</div>
)


Both of there are in the same Landing.js file.



I know my problem is that i try to do this.setState outside the Landing class.
Are there any solutions for this problem? Or is there a better way i should program this?



I've also read some things about redux and contexts in react. Which of these is my best way to go? Or are there more easy solutions?


More From » reactjs

 Answers
11

Short answer: No, you cannot setState outside a component.



Long answer: You cannot directly modify the component's state outside it, but you can always create a method that changes the state, then pass it down as props to <Code /> component. Example code (<Link /> component has been removed for simplicity)



import React, { Component } from react;
import ReactDOM from react-dom;

class Landing extends Component {
state = {
loginMethod: ,
tournamentCode: -1
};

onChange = e => {
this.setState({
tournamentCode: e.target.value
});
};

render() {
//You can use <></> or <React.Fragment></React.Fragment>
//I printed out the state to show you that it has been updated
return (
<>
<div>{this.state.tournamentCode}</div>
<Code onChange={this.onChange} />
</>
);
}
}

const Code = ({ onChange }) => (
<div style={{ marginTop: 20 }}>
<input
style={{ lineHeight: 1, height: 30, border: 0, paddingLeft: 5 }}
placeholder=Tournament Code
onChange={onChange}
/>
<button
style={{
marginLeft: 10,
border: 0,
outline: none,
background: #efefef,
height: 30,
width: 80
}}
>
Enter
</button>
</div>
);

const rootElement = document.getElementById(root);
ReactDOM.render(<Landing />, rootElement);


Important: No Redux or context is needed. Learn the basics and try thinking of the most simple solution to a problem, then extend it further. Not in this case, but that's a good learning approach you should apply



Codesandbox for future reference: https://codesandbox.io/s/n1zj5xm24


[#52992] Wednesday, November 28, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
travion

Total Points: 137
Total Questions: 96
Total Answers: 103

Location: India
Member since Wed, Aug 4, 2021
3 Years ago
travion questions
Mon, Dec 16, 19, 00:00, 5 Years ago
Sat, Oct 19, 19, 00:00, 5 Years ago
Fri, Sep 20, 19, 00:00, 5 Years ago
Wed, Nov 14, 18, 00:00, 6 Years ago
Sun, Oct 28, 18, 00:00, 6 Years ago
;