简体   繁体   English

如何使用react将输入标签中的字符串存储在变量中?

[英]How to store a string from input tag in a variable using react?

I'm a beginner react js user and I want to store a string from input tag in a variable.我是一个初学者反应 js 用户,我想将输入标签中的字符串存储在变量中。 Then I want the string stored in the variable to be connected with API URL as shown below然后我希望存储在变量中的字符串与 API URL 连接,如下所示

` `

const [stats, setStats] = useState([]);

const ID="";

useEffect(() => {
    axios
      .get(`https://fortnite-api.com/v2/stats/br/v2/${ID}`)
      .then(res => {
        setStats(res.data);
        console.log(res.data);
      })
      .catch(error => console.log(error));
  }, []);

return (
    <div>
       <h1>Search a Player</h1>
    <form>
      <input
        type='text'
        //onChange={}
        placeholder='Search'
      />
    </form>
    </div>
)

Your help will make my day.你的帮助会让我开心。

You will have to save your ID as a state variable.您必须将ID保存为状态变量。 You can thencontrol its value based on your onChange event handler.然后,您可以根据onChange事件处理程序控制其值。

const [stats, setStats] = useState([]);
const [ID,setID] = useState('');

const changeId = (event) =>{
setID(event.target.value);
}

useEffect(() => {
if(ID!='') {
    axios
      .get(`https://fortnite-api.com/v2/stats/br/v2/${ID}`)
      .then(res => {
        setStats(res.data);
        console.log(res.data);
      })
      .catch(error => console.log(error));
  }
}, [ID]);

return (
    <div>
       <h1>Search a Player</h1>
    <form>
      <input
        type='text'
        onChange={(event) => {changeId(event.target.value) }
        placeholder='Search'
      />
    </form>
    </div>
)

Ensure, that your useEffect callback is only called when ID changes.确保您的useEffect回调仅在 ID 更改时调用。 So pass in ID in the dependency array .所以在依赖数组中传入ID With an empty array it is called only once after component has mounted.对于空数组,它仅在组件安装后调用一次。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在变量中存储input_tag的值? - How to store values ​of an input_tag in a variable? 如何<button>在React.js中</button>使用<button>标签</button>存储和传递用户输入值<button>?</button> - How to store and pass the user input value using <button> tag in React.js? 从输入框存储变量并在p标记中显示值 - Store variable from input box and display value in p tag 如何使用javascript从文本输入中将值存储为字符串 - How to store a value as a string from a text input using javascript 如何将jsp select标记中的值存储到输入标记中 - How to store a value from jsp select tag into input tag 如何存储用户输入值 state 从反应到表达 js 数组和变量? - how to store user input value state from react to express js array and variable? 如何使用html输入标签从用户获取图像并将其存储在树莓派上的文件夹中 - How to get an image from a user and store it in a folder on a raspberry pi using the html input tag 如何使用getElementsByTagName()方法获取body标签的内容并将其存储在变量中 - How to get content of body tag and store it in a variable by using getElementsByTagName() method 如何使用jQuery获取HTML标签ID的值并将其存储在变量中? - How to fetch the value of HTML tag id and store it in a variable using jQuery? 如何在字符串中呈现变量<text> React Native 中的 JSX 标签</text> - How to render variable in string in <Text> JSX tag in React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM