Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从axios.get返回更新useState的初始状态_Javascript_Node.js_Reactjs_React Router_Axios - Fatal编程技术网

Javascript 如何从axios.get返回更新useState的初始状态

Javascript 如何从axios.get返回更新useState的初始状态,javascript,node.js,reactjs,react-router,axios,Javascript,Node.js,Reactjs,React Router,Axios,那里!嗯,我正试图为我的个人项目建立一个更新页面,在这个页面上,我想知道关于某个特定地点的所有信息。我将信息存储在MongoDB集群上,并通过axios.get请求获取信息 让我更好地解释一下: export default function({ history }) { window.data = {} const url = window.location.href.slice(-24) useEffect(() => { async fun

那里!嗯,我正试图为我的个人项目建立一个更新页面,在这个页面上,我想知道关于某个特定地点的所有信息。我将信息存储在MongoDB集群上,并通过axios.get请求获取信息

让我更好地解释一下:

export default function({ history }) {

    window.data = {}
    const url = window.location.href.slice(-24)

    useEffect(() => {
        async function setParams() {
            const { data } = await api.get(`/spots/${url}`)

            console.log(data)
            window.data = data
            }
            setParams()
        }, [url]) 

        console.log(window.data)

//I'd like to put the window.data data inside of this 
//useStates, so I will display into the form inputs the 
//previous information to the user update it in the backend. 

        const [thumbnail, setThumbnail] = useState(null)<---
        const [company, setCompany] = useState('')<---
        const [price, setPrice] = useState('')<---
        const [techs, setTechs] = useState('')<---


    const preview = useMemo(() => {
        return thumbnail ? URL.createObjectURL(thumbnail) : null;
    }, [thumbnail])

    async function handleSubmit(event) {
        event.preventDefault()

        const data = new FormData()
        const user_id = localStorage.getItem('user')

        data.append('thumbnail', thumbnail)
        data.append('company', company)
        data.append('techs', techs)
        data.append('price', price)

        await api.put(`/updates/${url}`, data, { new: true })

        history.push('/dashboard')
    }
    return (
        <form onSubmit={handleSubmit}>
            <label 
            id='thumbnail' 
            style={{ backgroundImage: `url(${preview})` }}
            className={thumbnail ? 'has-thumbnail' : ''}
            >
                <input type='file' onChange={event => setThumbnail(event.target.files[0])}/>
                <img src={camera} alt='Select img' />
            </label>

            <label htmlFor="company">EMPRESA *</label>
            <input
                id="company"
                placeholder="Sua empresa"
                value={company}//I'd like to fill this inputs with the information that I got from the get request.
                onChange={event => setCompany(event.target.value)}
            />

            <label htmlFor="company">TECNOLOGIAS *<span>(separadas por vírgula)</span></label>
            <input
                id="techs"
                placeholder="Tenologias utilizadas"
                value={techs}
                onChange={event => setTechs(event.target.value)}
            />

            <label htmlFor="company">PREÇO *</label>
            <input
                id="price"
                placeholder="Valor por dia"
                value={price}
                onChange={event => setPrice(event.target.value)}
            />

            <button type='submit' className='btn'>Cadastrar</button>
        </form>
    )
}
导出默认函数({history}){
window.data={}
常量url=window.location.href.slice(-24)
useffect(()=>{
异步函数setParams(){
const{data}=await api.get(`/spots/${url}`)
console.log(数据)
window.data=数据
}
setParams()
},[url])
console.log(window.data)
//我想把window.data数据放在这里面
//useStates,因此我将在表单中显示输入
//用户以前的信息将在后端更新。

const[thumbnail,setThumbnail]=useState(null)我想您可以调用
useffect()
after
axios
返回数据,例如
setThumbnail
setPrice
在您的案例中需要设置
thumbnail
price
状态值的数据。但是我将如何使用它之后的值?它们不会被锁定在useEffect范围内吗?不,它们不会。因此技术上发生了变化
useffect()中的状态
很好,您仍然可以在
渲染中访问修改后的状态。我建议您阅读以下内容:但是
useffect
中有一个箭头函数,因此它创建了自己的作用域。@WennlysOliveira是的,
useState
更新程序函数在功能组件的作用域中,因此它们是c同一范围内的任何其他对象都不能调用。