reactjs

How to add onclick event to a string rendered by dangerouslysetInnerHtml in reactjs?

岁酱吖の 提交于 2021-02-16 09:47:08
问题 I have a string , i.e, let string= "Hello <b>Click here</b>"; render() { return (<div dangerouslySetInnerHTML={this.createMarkup(value)}/> } createMarkup = value => { return { __html: value }; }; What I would like is to be able to add an onclick event to the <b> tag perform state manipulations on click. The underlying problem is where I had a function which was supposed to render whatever is passed by the API. The API would send a string 'Money received for order ID 123 ', or could be any

REACT : How can we Upload Image and Input text using Fetch or Axios in a single click function

落花浮王杯 提交于 2021-02-16 09:36:06
问题 So basically I'm new to react and couldn't find a single doc or resources regarding uploading an Image and input value at the same time. One Solution is Form Data , But that isn't working as expected The other method is Serialize , But I can't find any doc that explain the way to use in React So, it would be really great for me and the newbies of React to know about this if you anyone could help me out. 回答1: You can try the following method you can use multer with express to handle the file

React useEffect infinite loop fetch data axios

為{幸葍}努か 提交于 2021-02-16 09:13:52
问题 I'm getting an infinite loop with this code. I've been trying some of the solutions from another posts but they don't work. locationAddress is an array of addresses and I'm trying to get the coordinates using the Google Maps Geocode API. const reducer = (state, action) => { switch (action.type) { case 'add': return [ ...state, { address: action.address, name: action.name, id: action.id } ]; case 'remove': return state.filter((_, index) => index !== action.index) default: return state; } }

React useEffect infinite loop fetch data axios

陌路散爱 提交于 2021-02-16 09:12:09
问题 I'm getting an infinite loop with this code. I've been trying some of the solutions from another posts but they don't work. locationAddress is an array of addresses and I'm trying to get the coordinates using the Google Maps Geocode API. const reducer = (state, action) => { switch (action.type) { case 'add': return [ ...state, { address: action.address, name: action.name, id: action.id } ]; case 'remove': return state.filter((_, index) => index !== action.index) default: return state; } }

How can I create a style element and append to head in React?

ぐ巨炮叔叔 提交于 2021-02-16 06:11:31
问题 I'm currently learning React, and (more importantly) attempting to learn how react actually works. I have some generated css which I would like to append to head as a style element. In js land, this would be: const $style = document.createElement("style"); document.head.appendChild($style); const randBlue = ~~(Math.random() * 250); $style.innerHtml = `body { color: rgb(10, 10, ${randBlue}); }`; Unfortunately, in React land, things appear to be a little less straightforward in this regard. My

My create-react-app is failing to compile due to ESLint error

有些话、适合烂在心里 提交于 2021-02-16 04:26:46
问题 I just created a fresh template with create-react-app with react v17 included, and I installed eslint dependencies as I used to before, here's my package.json file { "name": "gym-nation", "version": "0.1.0", "private": true, "dependencies": { "@testing-library/jest-dom": "^5.11.5", "@testing-library/react": "^11.1.0", "@testing-library/user-event": "^12.1.10", "axios": "^0.21.0", "classnames": "^2.2.6", "moment": "^2.29.1", "prop-types": "^15.7.2", "react": "^17.0.1", "react-dom": "^17.0.1",

Passing useState as props in typescript

杀马特。学长 韩版系。学妹 提交于 2021-02-15 11:44:18
问题 Say I have a parent component with two child components: const Parent = () => { const [myVar, setmyVar] = useState(false) return ( <> <MyChildComponent1 myVar={myVar} setMyVar={setMyVar} \> <MyChildComponent2 myVar={myVar} \> </> ) } Now how would I go about setting the type correctly in MyChildComponent2 ? This is what I've come up with so far: const MyChildComponent1 = ( {myVar, setMyVar}: {myVar: boolean, setMyVar: (value: boolean) => void}) = (...) Is the type for setMyvar correct? Or

Unable to send the multi part form data from React to Express Js correctly

为君一笑 提交于 2021-02-15 07:51:24
问题 I am trying to upload files using Dropzone and send these files to java back end API from React JS. Here React is sending the document to express back end and adding some keys and then sending the final form data to back end java endpoint. But not able to get the files and request part in the back end side. The express is getting the wrong format of the data Appreciate any help. UploadMyFiles.jsx const UploadMyFiles = () => { const [selectedFiles, setSelectedFiles]= useState(null) const

Render dynamic html in react js

蓝咒 提交于 2021-02-15 07:50:23
问题 I want to render a dynamic html text provide by a API in react js but I don't know how to do. for example : dynamicHtml= <div> hello <span color=red> StackOverFlow </span> </div> render() { return ( <div> {dynamicHtml} </div> ); } I want to dysplay only Hello StackOverFlow with the red color on StackOverFlow 回答1: use dangerouslySetInnerHTML function createMarkup(text) { return {__html: text}; }; <render() { return ( <div dangerouslySetInnerHTML={createMarkup()} /> ); } 来源: https:/

Where should I make AJAX and API calls in React.js function components?

拥有回忆 提交于 2021-02-15 07:39:37
问题 I've been learning more about React.js function components and have started transitioning one of my React.js applications to use them instead of the standard react components. In my react components I had been making AJAX/API call(s) in the componentDidMount() function. Since that function doesn't exist in function components I am unsure where to put them. I couldn't find the answer on the React.js site, the only page I could find on AJAX and APIs shows making those calls with react standard