Input text inside a react component












0















I'm trying to create an input text inside a react component and then I realised that it's a bad praxis. So I investigated a little bit so I found Controlled-Components, so I think this is what I need, but looking at my Component I do not know how to create it.



I do not have an extends Redux.Component so a friend suggested me to create a Component but couldn't get succeed.



What I was trying is this :



Inside my component



 <input
...
/>
{" "}
<input
...
/>
<span>
<myButton
...
arguments={[document.getElementById("id1").value, document.getElementById("id2").value]}
>
[ send ]
</myButton>{" "}
</span>


But I'm getting this error :




The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!




EDIT



On my component where I have all of those code I have this :



 <myButton
id={id}
arguments={[intputStuff]}
>


So my problem is if I do what Tom's says I do not have the id in the other component.



So the thing should be create this component inside the other component and then get the values of the inputtexts and put them as an arguments










share|improve this question




















  • 1





    That is not how reactjs works. Tom's answer is the correct react pattern.

    – Jules
    Nov 25 '18 at 21:48


















0















I'm trying to create an input text inside a react component and then I realised that it's a bad praxis. So I investigated a little bit so I found Controlled-Components, so I think this is what I need, but looking at my Component I do not know how to create it.



I do not have an extends Redux.Component so a friend suggested me to create a Component but couldn't get succeed.



What I was trying is this :



Inside my component



 <input
...
/>
{" "}
<input
...
/>
<span>
<myButton
...
arguments={[document.getElementById("id1").value, document.getElementById("id2").value]}
>
[ send ]
</myButton>{" "}
</span>


But I'm getting this error :




The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!




EDIT



On my component where I have all of those code I have this :



 <myButton
id={id}
arguments={[intputStuff]}
>


So my problem is if I do what Tom's says I do not have the id in the other component.



So the thing should be create this component inside the other component and then get the values of the inputtexts and put them as an arguments










share|improve this question




















  • 1





    That is not how reactjs works. Tom's answer is the correct react pattern.

    – Jules
    Nov 25 '18 at 21:48
















0












0








0








I'm trying to create an input text inside a react component and then I realised that it's a bad praxis. So I investigated a little bit so I found Controlled-Components, so I think this is what I need, but looking at my Component I do not know how to create it.



I do not have an extends Redux.Component so a friend suggested me to create a Component but couldn't get succeed.



What I was trying is this :



Inside my component



 <input
...
/>
{" "}
<input
...
/>
<span>
<myButton
...
arguments={[document.getElementById("id1").value, document.getElementById("id2").value]}
>
[ send ]
</myButton>{" "}
</span>


But I'm getting this error :




The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!




EDIT



On my component where I have all of those code I have this :



 <myButton
id={id}
arguments={[intputStuff]}
>


So my problem is if I do what Tom's says I do not have the id in the other component.



So the thing should be create this component inside the other component and then get the values of the inputtexts and put them as an arguments










share|improve this question
















I'm trying to create an input text inside a react component and then I realised that it's a bad praxis. So I investigated a little bit so I found Controlled-Components, so I think this is what I need, but looking at my Component I do not know how to create it.



I do not have an extends Redux.Component so a friend suggested me to create a Component but couldn't get succeed.



What I was trying is this :



Inside my component



 <input
...
/>
{" "}
<input
...
/>
<span>
<myButton
...
arguments={[document.getElementById("id1").value, document.getElementById("id2").value]}
>
[ send ]
</myButton>{" "}
</span>


But I'm getting this error :




The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!




EDIT



On my component where I have all of those code I have this :



 <myButton
id={id}
arguments={[intputStuff]}
>


So my problem is if I do what Tom's says I do not have the id in the other component.



So the thing should be create this component inside the other component and then get the values of the inputtexts and put them as an arguments







javascript reactjs redux react-redux






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 22:00







StuartDTO

















asked Nov 25 '18 at 21:20









StuartDTOStuartDTO

441324




441324








  • 1





    That is not how reactjs works. Tom's answer is the correct react pattern.

    – Jules
    Nov 25 '18 at 21:48
















  • 1





    That is not how reactjs works. Tom's answer is the correct react pattern.

    – Jules
    Nov 25 '18 at 21:48










1




1





That is not how reactjs works. Tom's answer is the correct react pattern.

– Jules
Nov 25 '18 at 21:48







That is not how reactjs works. Tom's answer is the correct react pattern.

– Jules
Nov 25 '18 at 21:48














1 Answer
1






active

oldest

votes


















1














It's not clear from your post what exactly you're trying to accomplish.



It appears that you're trying to build a component with 2 text inputs and a button.



If you want the button to "submit" the values of the two inputs, you should do something like this:



class SomeComponent extends React.Component {

constructor(props) {
super(props)

this.state = {
value1: props.initialValue1,
value2: props.initialValue2
}
}


onChangeText = (event) => this.setState({ [event.target.name]: event.target.value })


onClickSubmit = (event) => {
let { onSubmit } = this.props
if(typeof onSubmit !== 'function') return

let { value1, value2 } = this.state
return onSubmit([ value1, value2 ])
}


render() {
let {
initialValue1,
initialValue2,
onSubmit,
className,
...props
} = this.props

let {
value1,
value2
} = this.state

return (
<div className={`SomeComponent ${className}`} {...props}>
<input value={value1} name="value1" onChange={this.onChangeText} />

<input value={value2} name="value2" onChange={this.onChangeText} />

<button onClick={this.onClickSubmit}>
Submit
</button>
</div>
)
}
}


A few notes:




  1. This example uses a bunch of futuristic JS: destructuring, rest/spread, class properties, computed property names, and arrow functions. Each feature is being leveraged for a specific purpose, not just because they're cool. If your environment doesn't support some of these features, you'll need to find a workaround that makes good on some additional constraints.


  2. This is not a controlled component, but it does contain 2 controlled inputs. It uses the "initialValue" pattern: the owning component provides starting values, but is unaware of the blow-by-blow as the user types each character. The owning component is only notified of the new values when the button is clicked. This pattern can result in loss of data if the owner is re-rendered before the current value are submitted.


  3. Generally, when using React, you want to avoid using native DOM methods to access or manipulate elements. (There are plenty of exceptions, of course.) One reason you want to avoid native DOM methods is that component lifecycle methods might execute before the React renderer has actually updated the DOM -- so document.getElementById('someid') might return undefined.







share|improve this answer


























  • But I have already a button so I need to send those inputs as an argument to my button

    – StuartDTO
    Nov 25 '18 at 21:43











  • If you want to provide the button with the values of the two inputs, you can do that easily: <MyButton arguments={[ value1, value2 ]} />

    – Tom
    Nov 25 '18 at 21:48











  • Problem is, that I need 2 more sutff that are on my other component class is there any way to get the buttom to other class ?

    – StuartDTO
    Nov 25 '18 at 21:48













  • I think you are going to need provide more detail in your post. It's obvious from these comments that you have additional constraints and goals that are not described in sufficient detail for the rest of us to work through them. SO doesn't usually let people do a lot of back-and-forth tech support in comments. My advice: post your whole component, not just part of the content of your render method. Also, describe the API of your custom <MyButton> component: what props does it accept and what are their types.

    – Tom
    Nov 25 '18 at 21:53











  • Yes I've edited my question

    – StuartDTO
    Nov 25 '18 at 21:53











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472100%2finput-text-inside-a-react-component%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














It's not clear from your post what exactly you're trying to accomplish.



It appears that you're trying to build a component with 2 text inputs and a button.



If you want the button to "submit" the values of the two inputs, you should do something like this:



class SomeComponent extends React.Component {

constructor(props) {
super(props)

this.state = {
value1: props.initialValue1,
value2: props.initialValue2
}
}


onChangeText = (event) => this.setState({ [event.target.name]: event.target.value })


onClickSubmit = (event) => {
let { onSubmit } = this.props
if(typeof onSubmit !== 'function') return

let { value1, value2 } = this.state
return onSubmit([ value1, value2 ])
}


render() {
let {
initialValue1,
initialValue2,
onSubmit,
className,
...props
} = this.props

let {
value1,
value2
} = this.state

return (
<div className={`SomeComponent ${className}`} {...props}>
<input value={value1} name="value1" onChange={this.onChangeText} />

<input value={value2} name="value2" onChange={this.onChangeText} />

<button onClick={this.onClickSubmit}>
Submit
</button>
</div>
)
}
}


A few notes:




  1. This example uses a bunch of futuristic JS: destructuring, rest/spread, class properties, computed property names, and arrow functions. Each feature is being leveraged for a specific purpose, not just because they're cool. If your environment doesn't support some of these features, you'll need to find a workaround that makes good on some additional constraints.


  2. This is not a controlled component, but it does contain 2 controlled inputs. It uses the "initialValue" pattern: the owning component provides starting values, but is unaware of the blow-by-blow as the user types each character. The owning component is only notified of the new values when the button is clicked. This pattern can result in loss of data if the owner is re-rendered before the current value are submitted.


  3. Generally, when using React, you want to avoid using native DOM methods to access or manipulate elements. (There are plenty of exceptions, of course.) One reason you want to avoid native DOM methods is that component lifecycle methods might execute before the React renderer has actually updated the DOM -- so document.getElementById('someid') might return undefined.







share|improve this answer


























  • But I have already a button so I need to send those inputs as an argument to my button

    – StuartDTO
    Nov 25 '18 at 21:43











  • If you want to provide the button with the values of the two inputs, you can do that easily: <MyButton arguments={[ value1, value2 ]} />

    – Tom
    Nov 25 '18 at 21:48











  • Problem is, that I need 2 more sutff that are on my other component class is there any way to get the buttom to other class ?

    – StuartDTO
    Nov 25 '18 at 21:48













  • I think you are going to need provide more detail in your post. It's obvious from these comments that you have additional constraints and goals that are not described in sufficient detail for the rest of us to work through them. SO doesn't usually let people do a lot of back-and-forth tech support in comments. My advice: post your whole component, not just part of the content of your render method. Also, describe the API of your custom <MyButton> component: what props does it accept and what are their types.

    – Tom
    Nov 25 '18 at 21:53











  • Yes I've edited my question

    – StuartDTO
    Nov 25 '18 at 21:53
















1














It's not clear from your post what exactly you're trying to accomplish.



It appears that you're trying to build a component with 2 text inputs and a button.



If you want the button to "submit" the values of the two inputs, you should do something like this:



class SomeComponent extends React.Component {

constructor(props) {
super(props)

this.state = {
value1: props.initialValue1,
value2: props.initialValue2
}
}


onChangeText = (event) => this.setState({ [event.target.name]: event.target.value })


onClickSubmit = (event) => {
let { onSubmit } = this.props
if(typeof onSubmit !== 'function') return

let { value1, value2 } = this.state
return onSubmit([ value1, value2 ])
}


render() {
let {
initialValue1,
initialValue2,
onSubmit,
className,
...props
} = this.props

let {
value1,
value2
} = this.state

return (
<div className={`SomeComponent ${className}`} {...props}>
<input value={value1} name="value1" onChange={this.onChangeText} />

<input value={value2} name="value2" onChange={this.onChangeText} />

<button onClick={this.onClickSubmit}>
Submit
</button>
</div>
)
}
}


A few notes:




  1. This example uses a bunch of futuristic JS: destructuring, rest/spread, class properties, computed property names, and arrow functions. Each feature is being leveraged for a specific purpose, not just because they're cool. If your environment doesn't support some of these features, you'll need to find a workaround that makes good on some additional constraints.


  2. This is not a controlled component, but it does contain 2 controlled inputs. It uses the "initialValue" pattern: the owning component provides starting values, but is unaware of the blow-by-blow as the user types each character. The owning component is only notified of the new values when the button is clicked. This pattern can result in loss of data if the owner is re-rendered before the current value are submitted.


  3. Generally, when using React, you want to avoid using native DOM methods to access or manipulate elements. (There are plenty of exceptions, of course.) One reason you want to avoid native DOM methods is that component lifecycle methods might execute before the React renderer has actually updated the DOM -- so document.getElementById('someid') might return undefined.







share|improve this answer


























  • But I have already a button so I need to send those inputs as an argument to my button

    – StuartDTO
    Nov 25 '18 at 21:43











  • If you want to provide the button with the values of the two inputs, you can do that easily: <MyButton arguments={[ value1, value2 ]} />

    – Tom
    Nov 25 '18 at 21:48











  • Problem is, that I need 2 more sutff that are on my other component class is there any way to get the buttom to other class ?

    – StuartDTO
    Nov 25 '18 at 21:48













  • I think you are going to need provide more detail in your post. It's obvious from these comments that you have additional constraints and goals that are not described in sufficient detail for the rest of us to work through them. SO doesn't usually let people do a lot of back-and-forth tech support in comments. My advice: post your whole component, not just part of the content of your render method. Also, describe the API of your custom <MyButton> component: what props does it accept and what are their types.

    – Tom
    Nov 25 '18 at 21:53











  • Yes I've edited my question

    – StuartDTO
    Nov 25 '18 at 21:53














1












1








1







It's not clear from your post what exactly you're trying to accomplish.



It appears that you're trying to build a component with 2 text inputs and a button.



If you want the button to "submit" the values of the two inputs, you should do something like this:



class SomeComponent extends React.Component {

constructor(props) {
super(props)

this.state = {
value1: props.initialValue1,
value2: props.initialValue2
}
}


onChangeText = (event) => this.setState({ [event.target.name]: event.target.value })


onClickSubmit = (event) => {
let { onSubmit } = this.props
if(typeof onSubmit !== 'function') return

let { value1, value2 } = this.state
return onSubmit([ value1, value2 ])
}


render() {
let {
initialValue1,
initialValue2,
onSubmit,
className,
...props
} = this.props

let {
value1,
value2
} = this.state

return (
<div className={`SomeComponent ${className}`} {...props}>
<input value={value1} name="value1" onChange={this.onChangeText} />

<input value={value2} name="value2" onChange={this.onChangeText} />

<button onClick={this.onClickSubmit}>
Submit
</button>
</div>
)
}
}


A few notes:




  1. This example uses a bunch of futuristic JS: destructuring, rest/spread, class properties, computed property names, and arrow functions. Each feature is being leveraged for a specific purpose, not just because they're cool. If your environment doesn't support some of these features, you'll need to find a workaround that makes good on some additional constraints.


  2. This is not a controlled component, but it does contain 2 controlled inputs. It uses the "initialValue" pattern: the owning component provides starting values, but is unaware of the blow-by-blow as the user types each character. The owning component is only notified of the new values when the button is clicked. This pattern can result in loss of data if the owner is re-rendered before the current value are submitted.


  3. Generally, when using React, you want to avoid using native DOM methods to access or manipulate elements. (There are plenty of exceptions, of course.) One reason you want to avoid native DOM methods is that component lifecycle methods might execute before the React renderer has actually updated the DOM -- so document.getElementById('someid') might return undefined.







share|improve this answer















It's not clear from your post what exactly you're trying to accomplish.



It appears that you're trying to build a component with 2 text inputs and a button.



If you want the button to "submit" the values of the two inputs, you should do something like this:



class SomeComponent extends React.Component {

constructor(props) {
super(props)

this.state = {
value1: props.initialValue1,
value2: props.initialValue2
}
}


onChangeText = (event) => this.setState({ [event.target.name]: event.target.value })


onClickSubmit = (event) => {
let { onSubmit } = this.props
if(typeof onSubmit !== 'function') return

let { value1, value2 } = this.state
return onSubmit([ value1, value2 ])
}


render() {
let {
initialValue1,
initialValue2,
onSubmit,
className,
...props
} = this.props

let {
value1,
value2
} = this.state

return (
<div className={`SomeComponent ${className}`} {...props}>
<input value={value1} name="value1" onChange={this.onChangeText} />

<input value={value2} name="value2" onChange={this.onChangeText} />

<button onClick={this.onClickSubmit}>
Submit
</button>
</div>
)
}
}


A few notes:




  1. This example uses a bunch of futuristic JS: destructuring, rest/spread, class properties, computed property names, and arrow functions. Each feature is being leveraged for a specific purpose, not just because they're cool. If your environment doesn't support some of these features, you'll need to find a workaround that makes good on some additional constraints.


  2. This is not a controlled component, but it does contain 2 controlled inputs. It uses the "initialValue" pattern: the owning component provides starting values, but is unaware of the blow-by-blow as the user types each character. The owning component is only notified of the new values when the button is clicked. This pattern can result in loss of data if the owner is re-rendered before the current value are submitted.


  3. Generally, when using React, you want to avoid using native DOM methods to access or manipulate elements. (There are plenty of exceptions, of course.) One reason you want to avoid native DOM methods is that component lifecycle methods might execute before the React renderer has actually updated the DOM -- so document.getElementById('someid') might return undefined.








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 21:56

























answered Nov 25 '18 at 21:39









TomTom

2,57132338




2,57132338













  • But I have already a button so I need to send those inputs as an argument to my button

    – StuartDTO
    Nov 25 '18 at 21:43











  • If you want to provide the button with the values of the two inputs, you can do that easily: <MyButton arguments={[ value1, value2 ]} />

    – Tom
    Nov 25 '18 at 21:48











  • Problem is, that I need 2 more sutff that are on my other component class is there any way to get the buttom to other class ?

    – StuartDTO
    Nov 25 '18 at 21:48













  • I think you are going to need provide more detail in your post. It's obvious from these comments that you have additional constraints and goals that are not described in sufficient detail for the rest of us to work through them. SO doesn't usually let people do a lot of back-and-forth tech support in comments. My advice: post your whole component, not just part of the content of your render method. Also, describe the API of your custom <MyButton> component: what props does it accept and what are their types.

    – Tom
    Nov 25 '18 at 21:53











  • Yes I've edited my question

    – StuartDTO
    Nov 25 '18 at 21:53



















  • But I have already a button so I need to send those inputs as an argument to my button

    – StuartDTO
    Nov 25 '18 at 21:43











  • If you want to provide the button with the values of the two inputs, you can do that easily: <MyButton arguments={[ value1, value2 ]} />

    – Tom
    Nov 25 '18 at 21:48











  • Problem is, that I need 2 more sutff that are on my other component class is there any way to get the buttom to other class ?

    – StuartDTO
    Nov 25 '18 at 21:48













  • I think you are going to need provide more detail in your post. It's obvious from these comments that you have additional constraints and goals that are not described in sufficient detail for the rest of us to work through them. SO doesn't usually let people do a lot of back-and-forth tech support in comments. My advice: post your whole component, not just part of the content of your render method. Also, describe the API of your custom <MyButton> component: what props does it accept and what are their types.

    – Tom
    Nov 25 '18 at 21:53











  • Yes I've edited my question

    – StuartDTO
    Nov 25 '18 at 21:53

















But I have already a button so I need to send those inputs as an argument to my button

– StuartDTO
Nov 25 '18 at 21:43





But I have already a button so I need to send those inputs as an argument to my button

– StuartDTO
Nov 25 '18 at 21:43













If you want to provide the button with the values of the two inputs, you can do that easily: <MyButton arguments={[ value1, value2 ]} />

– Tom
Nov 25 '18 at 21:48





If you want to provide the button with the values of the two inputs, you can do that easily: <MyButton arguments={[ value1, value2 ]} />

– Tom
Nov 25 '18 at 21:48













Problem is, that I need 2 more sutff that are on my other component class is there any way to get the buttom to other class ?

– StuartDTO
Nov 25 '18 at 21:48







Problem is, that I need 2 more sutff that are on my other component class is there any way to get the buttom to other class ?

– StuartDTO
Nov 25 '18 at 21:48















I think you are going to need provide more detail in your post. It's obvious from these comments that you have additional constraints and goals that are not described in sufficient detail for the rest of us to work through them. SO doesn't usually let people do a lot of back-and-forth tech support in comments. My advice: post your whole component, not just part of the content of your render method. Also, describe the API of your custom <MyButton> component: what props does it accept and what are their types.

– Tom
Nov 25 '18 at 21:53





I think you are going to need provide more detail in your post. It's obvious from these comments that you have additional constraints and goals that are not described in sufficient detail for the rest of us to work through them. SO doesn't usually let people do a lot of back-and-forth tech support in comments. My advice: post your whole component, not just part of the content of your render method. Also, describe the API of your custom <MyButton> component: what props does it accept and what are their types.

– Tom
Nov 25 '18 at 21:53













Yes I've edited my question

– StuartDTO
Nov 25 '18 at 21:53





Yes I've edited my question

– StuartDTO
Nov 25 '18 at 21:53


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472100%2finput-text-inside-a-react-component%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)